1

我正在为我的 Web 应用程序使用 jQuery UI 自动完成插件。
它适用于我现有的行,但不适用于我动态添加的行。

这是我的jQuery代码。

$(function ()
{
    var tab = $("#tabs-6");
tab.find("input[name='newContactName']").autocomplete(
{
        source: function(request, response) {
            var cachedRequest = $(this).data("cachedRequest");
            // Previous data is cached, so new data is a subset of this
            if (cachedRequest != null && 
                    cachedRequest.length > 0 && 
                    request.term.indexOf(cachedRequest) >= 0)
            {
                ..some code..
            }
            else
            {
                var input = $(this);
                $.ajax({
                    ..some code..
                    success: function(data) 
                    {..some code..
                    }

                }); 

            }
        },
        minLength: 3,
        delay: 500
    }
);

tab.find("input[name='newContactName']").bind('autocompleteselect', function(event, ui) {
    $(this).prev("input[name='newContactId']").val(ui.item.person_id);
    }); 

/* Customizing the autocomplete suggestions box that pops up.
*/
var input1=$("input[name='newContactName']"); 
input1.each(function (){
$(this).autocomplete("widget").css("height", "10em");
$(this).autocomplete("widget").css("overflow", "auto"); 
$(this).autocomplete("widget").css("width", "210px");
});

});

这是现有行的自动完成插件的 jQuery 代码,对于新添加的行,这是我要插入的 html

var html = '<tr>..first 8 td elements.. <td><input type="text" name="newContactName"> </td>'+
        ..some more td elements</tr>';

var newRow = $(html).insertAfter(row); // insert content,where row is my current row
newRow.find("td:nth-child(9) input[name='newContact']").autocomplete();

我如何确保自动完成功能也适用于我新添加的行?

4

1 回答 1

1

我看到了一些可能是错误的事情。

首先,当您添加新行时,将输入的名称设置为“newContactName”,但在 find() 时查找“name='newContact'”。

此外,在 find() 的结果上调用 autocomplete() 时,您需要指定 source 选项。由于您已将源设置为非动态行中的函数,因此您可能希望将其分解为命名函数并在两个 autocomplete() 调用中使用命名函数作为源。

希望这可以帮助...

于 2010-08-28T20:39:49.090 回答