Jquery 自动完成循环
我想将自动完成附加到动态生成的输入框。 我可以对那些由外部事件(例如单击按钮)生成的事件执行此操作。 但 在所需的情况下,输入框是在前一个输入框的 ONSELECT 事件上生成的,该输入框附加了自动完成功能。
换句话说,最初我的页面上有一个输入框(带有自动完成功能)。当选择(ONSELECT)其中一个选项时,它会生成另一个输入框。问题 - 生成的输入框还应该附加自动完成功能,其 ONSELECT 事件应该生成另一个输入框,并且这应该永远持续下去。
我主要需要这背后的逻辑。我对语法没问题。只是不知道如何解决循环问题。
我已阅读此处的其他问题,这些问题需要完成相同的操作,但在他们的情况下,元素是在外部生成的。非常感谢任何帮助。提前致谢。
编辑-我是新来的,不知道如何发布格式化的代码- @罗布 --- 谢谢。我喜欢你的想法。但我最终不明白你为什么使用 .val('') 。好吧,它仍然不起作用。我也尝试了其他类似的方法(例如将克隆附加到父元素(div#in))。警报显示在选择上。这意味着 onselect 事件被触发。这是代码 -
$.post("returncategory.php",
{},
function(data)
{
$('.cat').autocomplete({
source: data,
type:'json',
minchar: 1,
fillin: false,
onSelect: function() {
//alert($(this).parent().get());
//$(this).clone(true, true).appendTo($(this).parent());
//$(this).clone(true, true).appendTo('#in');
$(this).clone(true, true).insertAfter($(this)).val();
}
});
},
"JSON");
编辑:更正代码格式
编辑:我找到了解决方案:)非常感谢您的输入罗布! 这就是我所做的 -
$('.cat').live('click', function() {
$(this).autocomplete({
url: "returncategory.php",
type: "json",
minchar: 1,
onSuggest: function() {
},
onSelect: function() {
$('#clicker').click();
}
});
$(this).focus();
});
$('#clicker').bind('click', function() {
var newItem = $("<br><br><input class='cat' type='text'name='td_products["+1+"]'/>");
$('#in').append(newItem);
newItem.find('input').autocomplete({
url: "returncategory.php",
type: "json",
minchar: 1,
onSuggest: function() {
},
onSelect: function() {
$('#clicker').click();
}
});
});
PS我很高兴。现在我也知道如何在这里发布格式化代码。谢谢!
I want to attach autocomplete to dynamically generated input boxes.
I am able to do this with those who are generated from outside events(such as the click of a button).
BUT
in the required case, the input boxes are being generated on the ONSELECT event of a previous input box which has autocomplete attached to it.
In other words, initially I have an input box(with autocomeplete) on my page. When an option is selected(ONSELECT) into it, it generates another input box. PROBLEM-The generated input box should also have autocomplete attached to it, whose ONSELECT event should generate another input box, and this should go on forever.
I mainly need the logic behind this. I am ok with syntax. Just don't know how to approach the problem of looping.
I have read the other questions here which need the same thing done, but in their case, the elements are being generated externally. Would really appreciate any help. Thanks in advance.
EDIT- I am new here don't know how to post the code formatted-
@ Rob --- Thanks. I liked your idea. But I didn't get why you have used .val('') in the end. Well, it's still not working. I tried other similar ways too(like appending the clones to the parent element (div#in)). the alerts are showing up on select. So that means the onselect event is being fired. Here's the code --
$.post("returncategory.php",
{},
function(data)
{
$('.cat').autocomplete({
source: data,
type:'json',
minchar: 1,
fillin: false,
onSelect: function() {
//alert($(this).parent().get());
//$(this).clone(true, true).appendTo($(this).parent());
//$(this).clone(true, true).appendTo('#in');
$(this).clone(true, true).insertAfter($(this)).val();
}
});
},
"JSON");
Edit: corrected code formatting
EDIT: I found the solution :) Thanks a lot for your input Rob !!
Here's what i did -
$('.cat').live('click', function() {
$(this).autocomplete({
url: "returncategory.php",
type: "json",
minchar: 1,
onSuggest: function() {
},
onSelect: function() {
$('#clicker').click();
}
});
$(this).focus();
});
$('#clicker').bind('click', function() {
var newItem = $("<br><br><input class='cat' type='text'name='td_products["+1+"]'/>");
$('#in').append(newItem);
newItem.find('input').autocomplete({
url: "returncategory.php",
type: "json",
minchar: 1,
onSuggest: function() {
},
onSelect: function() {
$('#clicker').click();
}
});
});
P.S. I am very happy. And now I also know how to post formatted code here. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说的是,在选择时,您应该获取对当前输入的 jQuery 引用,然后执行
$this.clone(true, true).insertAfter($this).val('')
http://api.jquery.com/clone/
I would say on select, you should grab the jQuery reference to the current input, then do
$this.clone(true, true).insertAfter($this).val('')
http://api.jquery.com/clone/