dongzai3917 2013-06-10 09:33
浏览 58
已采纳

自动完成动态创建的输入

Sorry for asking something that has been already asked by people (for instance here :

jQuery autocomplete for dynamically created inputs)

but i cannot make work it despite the help i found on internet. So, i need to use Jquery autocomplete with dynamically created inputs. 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);
         }
      }
  • 写回答

1条回答 默认 最新

  • dryift6733 2013-06-10 09:44
    关注

    You use fixed id "autoCompleteProjets1", I believe you want "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:

    var last_ligne = 0;
    

    Then, you don't increment the last_ligne variable, use this:

    var next_ligne = ++last_ligne;
    

    And most important: use firebug in firefox, or equivalent for other browsers, to find out what you really generate.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大