代码之家  ›  专栏  ›  技术社区  ›  Jan

jquery用户界面的自动完成选择回调有问题(1.8)

  •  0
  • Jan  · 技术社区  · 16 年前

    用这个代码:

    function setupRow(event, ui) {
            var textbox, // how do i get to the textbox that triggered this? from there
            // on i can find these neighbours:
            hiddenField = textbox.next(), 
            select = textbox.parents('tr').find('select');
    
            textbox.val(ui.item.Name);
            hiddenField.val(ui.item.Id);
            $.each(ui.item.Uoms, function(i, item){
                select.append($('<option>' + item + '</option>'));
            });             
            return false;
        }
        function setupAutoComplete(){
            var serviceUrl = "/inventory/items/suggest";
            $("input.inputInvItemName").autocomplete({
                source: function(request, response) {
                    $.ajax({
                        url: serviceUrl,
                        data: request,
                        dataType: "json",
                        success: function(data) {
                            response($.map(data.InventoryItems, function(item) {
                                return {
                                    value: item.Name
                                };
                            }));
                        },
                        select: function(event, ui) {
                            setupRow(event, ui);
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            alert(textStatus);
                        }
                    });
                },
                minLength: 3,
                delay: 500
            });
        }
    

    似乎一切正常。问题是选择处理程序从不触发,甚至不调用为调试目的包装原始委托设置行的匿名函数。

    有人能看到我的错误吗?

    我在评论中还留了一个问题:我如何到达有自动提示的文本框。无法在此处使用ID,因为这些文本框是多个文本框,可以动态交互生成。还是有其他方法来做同样的事情?

    谢谢你的帮助!

    3 回复  |  直到 16 年前
        1
  •  1
  •   Reigel Gallarde    16 年前

    操作视角

    var textbox, // how do i get to the textbox that triggered this? from there
            // on i can find these neighbours:
    

    我的观点

    你试过了吗?

    var textbox = $(event.target);
    

    或者你可以这样做,

    操作视角

    select: function(event, ui) {
           setupRow(event, ui);
    },
    

    我的观点

    select: setupRow;
    

    然后

    var textbox = this; // just a guess... wait.. 
    
        2
  •  0
  •   Kai    16 年前
    anyone can see my error?
    

    我想你忘了放“;”。

    $.ajax({
                        url: serviceUrl,
                        data: request,
                        dataType: "json",
                        success: function(data) {
                            response($.map(data.InventoryItems, function(item) {
                                return {
                                    value: item.Name
                                }
                            }));
    

    还是有其他方法来做同样的事情?

    我认为你正在使用jquery u i自动完成插件。如果是,你可以这样检索。

    $('.ui-autocomplete-input')
    

    否则,可以为这些文本框设置特定的类,并通过该类访问这些文本框。

        3
  •  0
  •   Jan    16 年前

    好的,用

    inputs.bind("autocompleteselect", setupRow);
    

    现在设置行启动。

    现在看来,成功回调转换了数据,我得到了返回。我需要找到一种方法,在下拉列表中同时显示正确的值,而不破坏请求响应…

    有什么想法吗?