简体   繁体   English

自动完成动态创建的输入

[英]Autocomplete for dynamically created input

Sorry for asking something that has been already asked by people (for instance here : 很抱歉问到别人已经问过的问题(例如:

jQuery autocomplete for dynamically created inputs ) jQuery自动完成以动态创建输入

but i cannot make work it despite the help i found on internet. 但是,尽管我在互联网上找到了帮助,但我还是无法完成工作。 So, i need to use Jquery autocomplete with dynamically created inputs. 因此,我需要对动态创建的输入使用Jquery自动完成功能。 My code looks as follows: 我的代码如下所示:

$("#add_ligne2").live("click", function() { ...
         if (nb_ligne < 10) {
            var html = "";
            var next_ligne = last_ligne;
            html = '<tr rel="' + next_ligne + '">';
            html += '<td><input type="text" id="autoCompleteProjets' + next_ligne + '"/></td>';
            html += '</tr>';
            $("#content_tr").append(html);
            $('#autoCompleteProjets1', html).autocomplete(autocomp_opt);
         }
      }
      var autocomp_opt = {
         source: "/index/autocomplete",
         minLength: 2,
         select: function(event, ui) {
            /* console.log( ui.item ?
                 "Selected: " + ui.item.value + " aka " + ui.item.id :
                 "Nothing selected, input was " + this.value 
             );*/
            $('.hidden').val(ui.item.id);
         }
      }

You use fixed id "autoCompleteProjets1", I believe you want "autoCompleteProjets" + next_ligne 您使用固定ID“ autoCompleteProjets1”,我相信您想要“ autoCompleteProjets” + next_ligne

$('#autoCompleteProjets' + next_ligne, html).autocomplete(autocomp_opt);

You might already have it, but I'll mention it anyway - make sure you initialize last_ligne outside of the anonymous function, else you create local variable, which is unset once out of scope: 您可能已经有了它,但是无论如何我都会提到它-确保您在匿名函数之外初始化了last_ligne,否则您将创建局部变量,一旦超出范围,该局部变量将被取消设置:

var last_ligne = 0;

Then, you don't increment the last_ligne variable, use this: 然后,您无需增加last_ligne变量,请使用以下命令:

var next_ligne = ++last_ligne;

And most important: use firebug in firefox, or equivalent for other browsers, to find out what you really generate. 最重要的是:在firefox中使用firebug(或与其他浏览器等效的firebug)来找出真正生成的内容。

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

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