【发布时间】:2016-05-22 03:47:09
【问题描述】:
我正在使用 select2 jQuery 插件来制作我的选择框。如果我通过 jQuery 更新基本 select 字段,则 select2 项不会更新。 select2有什么办法可以监听原始select字段的变化,然后自行更新?
这是我的 HTML 代码:
<select name="myselect" id="myselect">
<option value=""></option>
<option value="a">A</option>
<option value="b">B</option>
</select>
默认将选择第一个选项(空选项)。这是我的 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
有没有办法自动更新select2元素?
【问题讨论】:
-
在更改
val()后使用$("select").trigger("change");。这也会更新您的select2()值。
标签: javascript jquery html ui-select