I'm using select2
jQuery
plugin to make fancy my select boxes. If I update the base select
field by jQuery the select2 item does not updated. Is there any way to the select2 to listen the original select field changes and then update itself?
我正在使用select2 jQuery插件来制作我喜欢的选择框。如果我通过jQuery更新基本选择字段,则select2项目不会更新。有没有办法让select2监听原始的选择字段更改然后更新自己?
Here is my HTML code:
这是我的HTML代码:
<select name="myselect" id="myselect">
<option value=""></option>
<option value="a">A</option>
<option value="b">B</option>
</select>
Default the first option (the empty one) will be selected. And here is my jQuery code:
默认选择第一个选项(空的选项)。这是我的jQuery代码:
$('select').select2(); // this line is running first always
// and there is no way to change the order
$('#myselect').val('a'); // this line is run after select2 built
// his elements
Is there any way to update the select2 element automatically?
有没有办法自动更新select2元素?
1 个解决方案
#1
0
Found the solution (just the documentation not call it "update"):
找到了解决方案(只是文档没有称之为“更新”):
$('#myselect').val('a').trigger('change');
#1
0
Found the solution (just the documentation not call it "update"):
找到了解决方案(只是文档没有称之为“更新”):
$('#myselect').val('a').trigger('change');