【发布时间】:2017-02-28 10:40:59
【问题描述】:
我对 jQuery select2 有一个很大的误解
我将 select2 添加到我的下拉列表中:
$('#PartnerAdresse_IdCountry').select2();
//#PartnerAddresse_IdCountry is filled with all countries
这是我创建 ASP MVC5 的下拉列表:
@Html.DropDownListFor(model => model.PartnerAdresse.IdCountry, ViewBag.Country as IEnumerable<SelectListItem>, "", new { @class = "form-control", style = "width:100%;" })
// Viewbag 是来自控制器的所有国家/地区的填充
但在我的页面上,什么都没有出现,这是我的控制台中生成的代码:
<select class="form-control" data-val="true" data-val-number="Le champ Pays doit être un nombre."
id="PartnerAdresse_IdCountry" name="PartnerAdresse.IdCountry" style="width:100%;">
<option value=""></option>
<option value="1">Afghanistan</option>
<option value="2">Afrique du Sud</option>
<option value="3">Albanie</option>
.
.
.
</select>
我也检查过:
- 页面中没有双重 id
- 使用 select2 可以正确生成其他表单
我只有在这个领域有问题...有人可以帮助我吗?
编辑 我已经更改,在我的代码中,使用 select2 更正了: $('#PartnerAdresse_IdCountry').select2();
但问题是一样的
【问题讨论】:
-
只需将
$('#PartnerAdresse_IdCountry').select();更改为$('#PartnerAdresse_IdCountry').select2(); -
是的,对不起,我弄错了,在我的代码中是:.select2()
-
我不确定选择名称中的
.点是有效标记。如果用下划线替换点会发生什么(使名称与 id 相同) -
从所有其他下拉列表中,select2 更改模型名称 (PartnerAdresse.idCountry) 并将 . (点)到一个下划线,以通过 Id 与 jQuery 一起使用
-
不要在 jquery 选择器中使用“生成的代码”。 使用本机 ID。 Select2 将隐藏您的选择并在其上创建一个新选择。它不会改变你的身份证或名字!!!
标签: javascript jquery html select select2