简体   繁体   English

jQuery MVC无法使用selected-select下拉列表

[英]chosen-select dropdown not working by jquery MVC

I'm using dropdown with multiple select with groups in my mvc razor page. 我在mvc剃须刀页面中将下拉列表与多个组一起使用。 I'm following below link and using Multiple Select with Groups dropdown. 我下面的链接,并使用多个选择组下拉菜单。

Harvest Multiselect Dropdown Link 收获多选下拉链接

It is working fine if I implement in razor view like below sample: 如果我在以下示例的剃刀视图中实施,效果很好:

....
<select data-placeholder="Search option" id="Mainlist[0].SubList" class="chosen-select" multiple tabindex="6" name="MainList[0].SubList">
     <option value=""></option>
     @foreach (var mainListItem in Model.MainList)
     {
          <optgroup label="@mainListItem.Name">
           @foreach (var subListItem in mainListItem.SubList)
           {
               <option id="report">@subListItem.Name</option>
           }
           </optgroup>
      }
</select>
....

Above razor code for chosen dropdown working fine like in below image: 上面的剃须刀代码对于选定的下拉菜单工作正常,如下图所示: 剃刀选择下拉列表

But I have a requirement to add more multi select dropdowns on button click event of jquery. 但是我需要在jquery的按钮单击事件上添加更多的multi select下拉列表。 I tried below code in jquery: 我在jquery中尝试了以下代码:

var newRow =
"<select data-placeholder='Search option' id='Mainlist[1].SubList' class='chosen-select' multiple tabindex='6' name='MainList[1].SubList'>" +
        "<option value=''></option>" +
        "<optgroup label='Main List Item 1'>" +
        "<option id='report'>Sub List Item 1.1</option>" +
        "<option id='report'>Sub List Item 1.2</option>" +
        "</optgroup>" +
        "<optgroup label='Main List Item 2'>" +
        "<option id='report'>Sub List Item 2.1</option>" +
        "<option id='report'>Sub List Item 2.2</option>" +
        "</optgroup>" +
        "</select>";

$('#TestTable tr:last').after(newRow);

First problem is : I can't implement foreach loop in newRow variable. 第一个问题是:我无法在newRow变量中实现foreach循环。 Second one is : If I try to manage whole list manually like above code, then also List is not coming properly like below image: 第二个是:如果我尝试像上面的代码一样手动管理整个列表,那么List也不能像下面的图像一样正常显示: jQuery的选择选择下拉

Any idea what is causing it? 知道是什么原因造成的吗? and how can I make dropdown working by jquery? 以及如何通过jquery使下拉菜单正常工作? or is there any other way to add same dropdown by jquery? 还是有其他方法可以通过jQuery添加相同的下拉菜单?

I already tried $(".chosen-select").chosen() from below link: 我已经从下面的链接尝试了$(“。chosen-select”)。chosen():

chosen-select link 选择选择链接

Check you are initializing the chosen after the select is added to dom. 将选择添加到dom后,检查是否正在初始化选择的内容。 Could you tell me the error you are facing. 您能告诉我您遇到的错误吗?

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

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