简体   繁体   English

克隆的Select2在Asp.Net中不起作用

[英]Cloned Select2 is not working in Asp.Net

i have tried many resources : 我尝试了很多资源:

Cloned Select2 is not responding 克隆的Select2没有响应

select2 is not working select2不起作用

select2 is not working select2不起作用

Initialising select2 created dynamically 初始化动态创建的select2

How to execute select2() function on dynamically created select list? 如何在动态创建的选择列表上执行select2()函数?

Select2 not displayed when added dynamically for the first time 第一次动态添加时不显示Select2

Explanation: 说明:

View page is build up using Razor . 视图页面是使用Razor构建的。 i'm trying to clone the row using JQuery, first one is working properly and newly created( dynamic) is not opening (freezed). 我正在尝试使用JQuery克隆row ,第一个工作正常,并且新创建的(动态)未打开(冻结)。 Image: image link 图片: 图片链接

View Code: 查看代码:

  <div id="ROW_0" class="template"> <hr /> <div class="row"> <div class="col-md-6"> <div class="form-group"> <label>Credit Account</label> @Html.DropDownListFor(model => model.AccountsId, new SelectList(Model.Accountss, "Id", "Name"), "-- select account --", new { @class = "form-control select required select2insidemodal", @id = "creditAccount" }) </div> </div> <div class="col-md-6"> <div class="form-group"> <label>Amount</label> @Html.EditorFor(model => model.Credit, new { htmlAttributes = new { @class = "form-control", @type = "text", @id = "credit" } }) </div> <span id="error-@Html.NameFor(vm => vm.Credit)" class="dN cR"></span> </div> </div> </div> 

Script: 脚本:

 //button for adding row $(document).ready(function () { $(document).on('click', "#addMore",function (e) { addNewRow(); }); function addNewRow() { var div = $(".template").clone().html(); //find all select2 and destroy them $(div).find(".select2").each(function (index) { if ($(this).data('select2')) { $(this).select2('destroy'); } }); $("#dynamicBlock").prepend(div); //adding cloned data to new div '#dynamicBlock'. 

Help me out. 帮帮我。

It doesn't look like you are initialising the new select's after you have appended them to the page. 将新选择项附加到页面后,您似乎无法对其进行初始化。

You will need to append new script to the page (and run it), to setup the select's, otherwise they will just sit there inert. 您将需要在页面上添加新脚本(并运行它),以设置所选内容,否则它们将处于惰性状态。

var initScript = '$(".select2insidemodal").select2();';

$('<script>' + initScript + '</' + 'script>').appendTo(document.body);

If you are going to clone more than one select, then I guess you could just add the init script last and only once, as it should init every cloned select. 如果您要克隆多个选择,那么我想您可以仅在最后一次添加init脚本一次,因为它应该初始化每个克隆的选择。

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

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