动态输入文本框未绑定到自动完成

时间:2011-10-21 22:54:35

标签: javascript jquery jquery-ui autocomplete jquery-autocomplete

我正在尝试遵循此解决方案,但没有得到任何运气。 jQuery autocomplete for dynamically created inputs

var autocomp_opt = {
    source: function (request, response) {

    $.ajax({
        url: "ServiceProxy.asmx/ByKeyWord",
        cache: false,
        data: "{ 'term':'" + request.term + "'}",
        dataType: "json",
        type: "POST",
        crossDomain: "true",
        contentType: "application/json; charset=utf-8",
        error: function (xhr, textStatus, errorThrown) {
            alert('Error: ' + xhr.responseText);
        },
        success: function (data) {
            response($.map(data, function (item) {
                return item.split(",");
            }));       
        },

        error: function (a, b, c) {

        }
    });
},
    minLength: 2
};

然后当我的输入框生成时,我试了......

    input = document.createElement("input");
    input.disabled = true;  
    input.className = this.FORM_INPUT_CLASS;

    $(input.id).autocomplete(autocomp_opt);
    table.rows[table.rows.length - 1].cells[1].appendChild(input);

没有错误,但它似乎没有正确绑定...如果有人有任何想法 - 请发布。感谢。

2 个答案:

答案 0 :(得分:3)

尝试更改此行:

$(input.id).autocomplete(autocomp_opt);

到此:

$('#' + input.id).autocomplete(autocomp_opt);

答案 1 :(得分:0)

我添加时可以使用:

    $('#fieldname").autocomplete(autocomp_opt);

这一行之后

    table.rows[table.rows.length - 1].cells[1].appendChild(input);

我猜它只能在输入框附加到某个东西时注册。值得注意的是,添加某种事件处理onfocus,onselect,即......,它也有效。虽然我怀疑,但这是因为此时输入框已完全渲染/添加。

相关问题