自动完成动态元素

时间:2017-02-23 15:42:00

标签: javascript php jquery autocomplete

我每次都在努力,今天又来了。 我附加了新的文本元素,我想将自动完成绑定到它。

此代码与之前的onClick

一起使用
$('#highlights').before("<input type='text' id='check_name' name='name' placeholder='Name of OPM' required class='ui-autocomplete-input'>");

此自动填充功能位于网页末尾

<script type="text/javascript">
$("#check_name").autocomplete({
      source: "src/check_name.php",
      minLength: 1
    });
</script>

当我通过URL访问它时,PHP文件运行良好。

2 个答案:

答案 0 :(得分:1)

  

因为.before():在匹配元素集合中的每个元素之前插入由参数指定的内容。

您可以使用.prev()

摘录:

&#13;
&#13;
    PracticeId  LastName  EntityId LocId  EntityName  LocName  PracName
    53681       Thomas    3194     2222   ECS Tampa   NULL     NULL
    53681       Thomas    3195     2222   ECS Atlanta NULL     NULL
    53681       Thomas    3222     2222   ES Hou      NULL     NULL
    53681       Thomas    3222     2434   ES Hou      ECS Hou  Regional Med
&#13;
var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
  ];


  $('#highlights').before("<input type='text' id='check_name' name='name' placeholder='Name of OPM' required class='ui-autocomplete-input'>")
        .prev("#check_name")  // get the newly prev added ele
        .autocomplete({
            source: availableTags,
            minLength: 1
        });
&#13;
&#13;
&#13;

答案 1 :(得分:1)

尝试这种方式来处理动态控制,

var $addedInput=$("<input type='text' id='check_name' name='name' placeholder='Name of OPM' required class='ui-autocomplete-input'>");
$('#highlights').before($addedInput);
$addedInput.autocomplete({
  source: "src/check_name.php",
  minLength: 1
});