jquery尝试绑定到稍后存在的元素

时间:2015-05-26 13:10:20

标签: jquery

当下拉菜单改变时,我正在创建一个输入文本框:

var html;

$('#communtiyDropdown').on('change', function () {
      $.ajax({
            type: "GET",
            url: "/vendorProject/api/connection/getVendorItems?community=" + $("#communtiyDropdown").val(),
            dataType: 'json',
            cache: false,
            success: function (results) {
                 $.each(results, function (key, value) {
                   html += "<td><input type='text' name='" + key + "-" + taskArray[i] + "' id='vendorDropdown' class='vendorDropdown' value='" + value.baseOrSchedStartList[i].split(' ')[0] + "' /></td>";
                 });
            }
            $("#tableData").html(html);
      });
});

然后我将自动填充添加到每个文本框中,如下所示:

var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];

    console.log(availableTags);

    $('.vendorDropdown').on('keyup', function () {
        console.log('here');
        $(this).autocomplete({
            source: availableTags
        });
    });

但是我的问题,自动完成功能不起作用,它没有出现,我的控制台日志中也没有出现错误......我做错了什么?

1 个答案:

答案 0 :(得分:0)

在你的成功回调中尝试一下,就像这样(也是要编入索引的固定ID):

success: function (results) {

    var html = '';

    $.each(results, function (key, value) {
        html += "<td><input type='text' name='" + key + "-" + taskArray[i] + "' id='vendorDropdown"+[i]+"' class='vendorDropdown' value='" + value.baseOrSchedStartList[i].split(' ')[0] + "' /></td>";
    });

    $("#tableData").html(html);
    $('.vendorDropdown').autocomplete({source: availableTags});
}