将select2下拉列表的值设置为空字符串
问题描述:
我已将我网站中的一些选择转换为select2,我注意到下面的代码不再有效。假设我有我的html:将select2下拉列表的值设置为空字符串
<select id = "CountryList" > </select>
然后
$("#CountryList").select2();
function Func() {
document.getElementById("CountryList").value = "";
}
Func();
什么是解决这个问题的最简单方法。
编辑:我想将选定的值设置为空字符串。
编辑2:$("#CountryList").select2("val", "");
编辑: 如果你只是想对任何人都需要这个作为未来的功能。如果你正在试图清空选择,使用我增加了一个答案
答
这工作:
function EmptyValueForSelect2(SelectId) {
$("#"+SelectId).val("").trigger("change");
}
function SetValueForSelect2(SelectId, Value) {
$("#"+SelectId).val(Value).trigger("change");
}
答
添加一个空字符串,并选择它:$("#CountryList").append('<option value="" selected="selected"></option>')
是你想删除的选择素t选项还是将选中的选项重置为默认值? –
@ H.C。我试图将select的选定值设置为空字符串。 – IntegrateThis
@ H.C不工作 – IntegrateThis