[英]Make a delegated jQuery Auto-complete
I am using the following code to create an auto-complete textbox. 我正在使用以下代码创建自动完成的文本框。 The jQuery is as follows. jQuery如下。
$(function() {
$( "#items .slno" ).autocomplete({
source: 'search.php'
});
});
The HTML is as follows. HTML如下。
<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. 但是,当通过在上面的代码中单击“添加一行”按钮创建第二行时(它们都正常工作),Auto_complete不能在随后的行中使用。 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? 那么如何将当前的jQuery选择器转换为委托选择器呢?
I Kinda wrote it like the following based on the answer, but it's still not working. 我Kinda根据答案将其写为以下内容,但仍无法正常工作。
$('#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? 我该如何实现?
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 !!! 它完成了任务!
The code below worked for me like a charm. 下面的代码对我来说很有吸引力。
$(function(){
$('#items').on('keyup.autocomplete','.slno', function(){
$(this).autocomplete({
source : 'search.php'
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.