Jquery AutoComplete无法在动态添加的表单字段中使用

时间:2014-04-04 06:44:11

标签: jquery autocomplete

我正在重复表单中的3个字段,所有字段都是autoComplete。

字段:

    <input type="text" class="input-block-level" Placeholder="College" name="College" />
    <input type="text" class="input-block-level" Placeholder="Course" name="Course" />
    <input type="text" class="input-block-level" Placeholder="Major" name="Major" />
    <a style="cursor: pointer" onclick="AddThisCollege()">Add More </a>

自动完成JS

  $( "input[name='College']" ).autocomplete({
    source: availableTags
  });

  $( "input[name='Course']" ).autocomplete({
    source: availableTags
  });

  $( "input[name='Major']" ).autocomplete({
    source: availableTags
  });

AddThisCollege()代码:

$("#CollegeListDiv").append('<br /><div class="CollegeList"> <input type="text" class="input-block-level" Placeholder="College" id="txtCollege" name="College" /> <input type="text" class="input-block-level" Placeholder="Course" id="txtCourse" name="Course" /> <input type="text" class="input-block-level" Placeholder="Major" id="txtMajor" name="Major" /> </div>');

自动完成适用于前三个类,它不适用于动态添加的字段。

1 个答案:

答案 0 :(得分:3)

在AddThisCollege函数中添加自动完成功能。只有附加到动态元素的事件

试试这个

 function AddThisCollege(){

   $("#CollegeListDiv").append('<br /><div class="CollegeList"> <input type="text" class="input-block-level" Placeholder="College" id="txtCollege" name="College" /> <input type="text" class="input-block-level" Placeholder="Course" id="txtCourse" name="Course" /> <input type="text" class="input-block-level" Placeholder="Major" id="txtMajor" name="Major" /> </div>');
    $( "input[name='College'],input[name='Course'],input[name='Major']" ).autocomplete({
        source: availableTags
      });
    }