dongyan1841 2016-01-28 13:58
浏览 27

制作委派的jQuery自动完成

I am using the following code to create an auto-complete textbox. The jQuery is as follows.

$(function() {
   $( "#items .slno" ).autocomplete({
      source: 'search.php'
   });
}); 

The HTML is as follows.

<table id="items">
     <tr class="item-row">
            <td class="item-name"><div class="delete-wpr"><a class="delete" href="javascript:;" title="Remove row">-</a></div></td>
            <td><input type="text" id="slslno" class="slno"/></td>
            <td><input type="text" class="cost"/></td>
            <td><input type="text" class="qty"/></td>
            <!--  <td><span class="price"></span></td>-->
            <td class="price"></td>
            <a class="add" id="addrow" href="javascript:;" title="Add a row">+</a> </tr>

</table>

The Auto-complete works for the first row. However, when the second row is created by clicking on the "Add a row" button in the above code (Its all working fine), the Auto_complete just isn't working on the subsequent rows. I sorted out that I would need a delegated handler for that. SO how can I convert my current jQuery selector to a delegated one?

I Kinda wrote it like the following based on the answer, but it's still not working.

   $('#items').delegate('#items .slno','keyup', function () {   

  $(this).autocomplete({
         source: 'search.php'
    });

  });

Then I went on to use the following.

  $(function(){
  $('#items').on('.slno','keyup.autocomplete', function(){
    $(this).autocomplete({
      source : 'search.php'
    });
  });
});

and it too failed. How do I achieve this?

UPDATE

In the above example, the ordering of the event was messed up. Just needed touse the proper syntax

$(selector).on(event,childSelector,data,function,map)

Which makes my code look like the following.

  $(function(){
  $('#items').on('keyup.autocomplete','.slno', function(){
    $(this).autocomplete({
      source : 'search.php'
    });
  });
});

And it did the task !!!

  • 写回答

1条回答 默认 最新

  • duanlv2788 2016-01-28 15:13
    关注

    The code below worked for me like a charm.

    $(function(){
          $('#items').on('keyup.autocomplete','.slno', function(){
            $(this).autocomplete({
              source : 'search.php'
            });
          });
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法