jquery auto complete不适用于动态生成的id

时间:2015-12-17 06:33:56

标签: jquery

我的代码看起来像

<div class="my-form">
  <p class="text-box">
  <input type="text" class="form-control" name="boxes[]" id="Name" value=""  placeholder="Name" onchange="javascript:callid(this.id);" />
  <input type="text" class="form-control" name="boxes[]" value="" id="box1" placeholder="Stripes" />
  <a class="add-box" href="#">Add More</a>
  </p>
 </div>
  <script type="text/javascript">
    function callid(id)
            $(document).ready(function()
            {
                $("#"+id+"").autocomplete(
                {
                    source:'autocomplete.php',
                    minLength:1
                });
            });

    </script>

1 个答案:

答案 0 :(得分:1)

试试这个:

  <div class="my-form">
      <p class="text-box">
      <input type="text" class="form-control" name="boxes[]" id="Name" value=""  placeholder="Name" />
      <input type="text" class="form-control" name="boxes[]" value="" id="box1" placeholder="Stripes" />
      <a class="add-box" href="#">Add More</a>
      </p>
     </div>
      <script type="text/javascript">
                $(document).ready(function()
                {
                    $('input[name="boxes[]"]').autocomplete(
                    {
                        source:'autocomplete.php',
                        minLength:1
                    });
                });
        </script>

注意:如果您已动态添加输入框,则需要为每个新添加的项目调用autcomplete