我将以下数据输入给 select2
data = [{
"id": "all",
"text": "All",
"children": [{
"id": "item1",
"text": "item1"
}, {
"id": "item2",
"text": "item2"
}]
}];
我将 select2 初始化为:
$(parent).select2({"data":data});
现在选择值“item1”,我这样做了
$(parent).select2("val",["item1"]);
但是,选择的不是值“item1”,而是值“all”。如何选择值item1?
最佳答案
要使用 select2
设置所选值,您应该使用:
$(parent).val("item1").trigger("change");
来自release notes :
You should directly call .val on the underlying element instead. If you needed the second parameter (triggerChange), you should also call .trigger("change") on the element.
$("select").val("1"); // instead of $("select").select2("val", "1");
关于javascript - 在具有 optGroup 的 select2 中选择一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30035577/