为动态创建的表行分配Jquery tokeninput

为动态创建的表行分配Jquery tokeninput,jquery,asp.net-mvc,jquery-tokeninput,Jquery,Asp.net Mvc,Jquery Tokeninput,已编辑 表格模板 <table id="Newdiagnosis" style="display:none"> <tr> <td><input class="diag" style="width:200px" type="text" name="provider_diagnosis_dtls[#].diagnosis_code" value /></td> <td><input style="width:500px"

已编辑

表格模板

<table id="Newdiagnosis" style="display:none">
<tr>
 <td><input class="diag" style="width:200px" type="text" name="provider_diagnosis_dtls[#].diagnosis_code" value /></td>
<td><input style="width:500px" type="text" name="provider_diagnosis_dtls[#].diagnosis_desc" value /></td>
<td>
<input type="text" name="provider_diagnosis_dtls[#].diagnosis_level)" readonly value />
<input type="hidden" name="diagnosis.Index" value="%" />
</td>
</tr>
 </table>
我需要为每个动态创建的文本框附加jquerytokeniput。可能吗

我在Jquery中尝试了以下代码

    $(document).ready(function () {
     $("#N").click(function () {
     var index = (new Date()).getTime();
     var clone = $('#Newdiagnosis').clone();
     clone.html($(clone).html().replace(/\[#\]/g, '[' + index + ']'));
    clone.html($(clone).html().replace(/"%"/g, '"' + index + '"'));
     $("#diagnosis").append(clone.html());
     });
 });
$("#N").click(function () {
    var index = (new Date()).getTime();
    var clone = $('#Newdiagnosis').clone();
    clone.html($(clone).html().replace(/\[#\]/g, '[' + index + ']'));
    clone.html($(clone).html().replace(/"%"/g, '"' + index + '"'));
    clone.find(".diag").tokenInput("@Url.Action("SearchDiagnosis", "Preapproval")", {
         theme: 'facebook',
         preventDuplicates: true,
         searchingText: 'Searching...',
         tokenLimit: 1
    });
    $("#diagnosis").append(clone.html());
});

这不是从url中给定的操作中搜索数据,而是对非动态文本框进行搜索。在元素添加到DOM后,您需要将插件附加到元素

var diagnosis = $("#diagnosis"); // cache it
$("#N").click(function () {
  var index = (new Date()).getTime();
  var clone = $('#Newdiagnosis').clone();
  clone.html($(clone).html().replace(/\[#\]/g, '[' + index + ']'));
  clone.html($(clone).html().replace(/"%"/g, '"' + index + '"'));
  diagnosis .append(clone.html());
  // Attach the plugin to the new element
  diagnosis.find('.diag').last().tokenInput('@Url.Action("SearchDiagnosis", "Preapproval")', {
    theme: 'facebook',
    preventDuplicates: true,
    searchingText: 'Searching...',
    tokenLimit: 1
  });
});
旁注:从您的评论中,您指出一些元素具有
onchange=“…”
。您应该从标记中删除行为,并使用事件委派来处理动态添加的元素

$("#diagnosis").on('change', '.diag', function() {
  // do something
});

您需要将插件附加到新元素。添加
id
属性的目的是什么?这篇文章怎么回复(你有重复的名字元素)?嗨,斯蒂芬,谢谢你的回复。我不会有重复的name元素,因为在元素末尾分配了唯一的id。发回我需要调查一下。任何其他可以实现这一点的简单方法。我是MVCYou的新成员,您确实有重复的名称元素!这不可能正确发回。
id
属性是不相关的,您应该使用
new{id=”“}
来删除id属性以避免无效的html。我建议您查看答案并了解如何动态生成集合项为动态创建的texbox附加jquery InputOken是可能的?当然,只需给元素一个类名,然后在
$(“#N”)。单击(函数()
-
clone.find('.theClassName')。tokenInput(..)