简体   繁体   English

动态输入框的jQuery自动完成器

[英]JQuery autocompleter for dynamic input boxes

I have a webpage with jquery generating dynamic html input boxes. 我有一个带有生成动态html输入框的jquery的网页。

Something like this appears on the page. 这样的事情出现在页面上。

<input type="text" id="numbers[]" ></input>
<input type="text" id="numbers[]" ></input>
<input type="text" id="numbers[]" ></input>
<input type="text" id="numbers[]" ></input>

These text-boxes all use the same autocompleter, is it possible in jQuery to point my autocompleter at all of these? 这些文本框都使用相同的自动完成程序,在jQuery中是否可以将我的自动完成程序指向所有这些?

First of all id should be unique in the whole document so your code is not correct. 首先,id在整个文档中应该是唯一的,因此您的代码不正确。

What you probably mean is 你可能的意思是

<input type="text" name="numbers[]" ></input>
<input type="text" name="numbers[]" ></input>
<input type="text" name="numbers[]" ></input>
<input type="text" name="numbers[]" ></input>

To enable autocomplete on those boxes just use the selector that will match them all 要在这些框上启用自动填充功能,只需使用与所有框都匹配的选择器

var data = "foo bar baz";
$('input[name^=numbers]').autocomplete(data);

You could add a div that wraps input and that is never changed, then upon creation of new input store its id in jquery internal cache, just like this: 您可以添加一个用于包装输入且从未更改的div,然后在创建新输入时将其ID存储在jquery内部缓存中,如下所示:

var $input = '<input name=somename[] type="text"/>';    
$('#mywrap').append($input);
$input.data('id', 'some id');

Then on you can access autocompleter in the following way: 然后,您可以通过以下方式访问自动完成程序:

$('#mywrap input').live('click', function() {
    var id = $(this).data('id');
    // and now do anything with the new id you have!
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM