【问题标题】:Autocomplete not working on dynamically added textBox自动完成不适用于动态添加的文本框
【发布时间】:2017-09-19 11:24:05
【问题描述】:

我找到了通过 jquery 创建动态文本框的代码,还发现了通过 javascript 在文本框上实现自动完成的代码。

但是将它们合并在一起对我来说是个问题。第一个文本框已成功实现自动完成,但通过添加按钮动态创建的新文本框不实现自动完成。这是我的代码

<script type="text/javascript">
$(function() {
    var availableTags = [
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"
    ];
    $(".insti_name").autocomplete({
        source: availableTags,
        autoFocus:true
    });
});

$(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('<br><div><input  class="insti_name" type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
</script>

<br><br>

<div class="input_fields_wrap">
    <button type="button" class="btn btn-primary btn-lg add_field_button "  > 
        ADD INSTITUTIONS
    </button><br>
    <div style="margin-top:11px">
        <input class="insti_name" type="text" name="mytext[]">
    </div>
</div>

【问题讨论】:

  • 在添加新元素后添加要绑定到类的事件处理程序。

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


【解决方案1】:

自动完成小部件 api 不支持您正在寻找的动态行为,但我们可以使用以下代码实现相同的功能:

$(add_button).click(function(e){ //on add input button click
    e.preventDefault();
    if(x < max_fields){ //max input box allowed
        x++; //text box increment

        // first destroy the previously setup auto-complete
        $( ".insti_name" ).autocomplete("destroy");

        // append your new html element
        $(wrapper).append('<br><div><input  class="insti_name" type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box

        // setup autocomplete again
        // you can move this call and the one above (the one you have set earlier) in one function for better maintainability
        $(".insti_name").autocomplete({
            source: availableTags,
            autoFocus:true
        });
    }
});

这肯定有效。希望对您有所帮助!

【讨论】:

  • @nitin 如果对您有用,请将其标记为解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-19
  • 2016-01-27
  • 1970-01-01
  • 2013-07-28
  • 2018-01-10
相关资源
最近更新 更多