我想在我的MVC项目的同一视图中使用javascript Id来获取一些DropDownListFor。但它只适用于其中一个。我需要做些什么来解决我的问题?
视图中的代码:
@Html.DropDownListFor(model => model.MemberInstance.MemberType_Id, new SelectList(Model.MemberTypeList, "Id", "Title"), new { @id = "demo-select2-1", @class = "form-control" })
@Html.DropDownListFor(model => model.SurgeryInstance.SurgeryType_Id, new SelectList(Model.SurgeryTypeList, "Id", "SurgeryTitle"), new { @id = "demo-select2-1", @class = "form-control" })
这只适用于第一个DropDownListFor而不是其他人。
答案 0 :(得分:0)
为了能够应用于多个元素,在这种情况下必须使用类Select2Demo
@Html.DropDownListFor(m=>m.First,new SelectList(Model.MemberTypeList, "Id", "Title"), new {@class="Select2Demo"} )
@Html.DropDownListFor(m=>m.Second,new SelectList(Model.MemberTypeList, "Id", "Title"), new {@class="Select2Demo"} )
@Html.DropDownListFor(m=>m.Third,new SelectList(Model.MemberTypeList, "Id", "Title"), new {@class="Select2Demo"} )
Selct2
$(".Select2Demo").select2({ placeholder: "Select a member" });
答案 1 :(得分:0)
首先我找到了带有id =" demo-select2-1"的java脚本部分。在主题的脚本中,然后我使用New Name重新定义相同的函数,并在其他DropDownListFor中使用新的id:
@Html.DropDownListFor(model => model.MemberInstance.MemberType_Id, new SelectList(Model.MemberTypeList, "Id", "Title"), new { @id = "demo-select2-1", @class = "form-control" })
@Html.DropDownListFor(model => model.SurgeryInstance.SurgeryType_Id, new SelectList(Model.SurgeryTypeList, "Id", "SurgeryTitle"), new { @id = "demo-select2-a", @class = "form-control" })
以及java脚本函数是
function r() {
var b = a("#demo-select2-1");
b.select2({
dir: "rtl"
})
}
我的新功能是
function r1() {
var b = a("#demo-select2-a");
b.select2({
dir: "rtl"
})
}
我也可以用同样的方式将它用于多个元素