如何为动态创建的文本框实现自动提示

时间:2017-05-18 10:09:39

标签: jquery autosuggest

我的代码如下。我是从网站上得到的。但我认为缺少一些东西。请帮我完成我的任务。提前致谢

// HTML

<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <div><input type="text" name="mytext[]"></div>
</div>

// JS

    var availableAttributes = [
          "account_address",
          "account_address_city",
          "account_address_country",
          "account_address_state",.
          "account_address_street1",
          "account_address_street2",
          "account_address_zip",
          "account_email",
          "account_login",
          "account_name",
          "account_number",
          "account_telephone"
        ];

    $(document).ready(function() {
        var max_fields      = 10; //maximum input boxes allowed
        var wrapper         = $(".input_fields_wrap"); //Fields wrapper
        var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><input id="' + x + '" type="text" name="mytext"/><a href="#" class="remove_field">Remove</a></div>');

 //add input box


 $( "input[id="+ x +"]" ).autocomplete({
                source: availableAttributes
            }); 
        }
    });

   });

1 个答案:

答案 0 :(得分:0)

问题非常严重。你的json错了。它在.

之后的json中有一个account_address_state

修复json后检查工作演示codepen