2016-05-21 43 views
0

我正在使用select2jQuery插件來製作我喜歡的選擇框。如果我通過jQuery更新基地select字段,則select2項不會更新。有沒有什麼辦法讓select2聽原始選擇字段的變化,然後自行更新?如何更新基於HTML選擇字段的select2下拉選定值

這裏是我的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 

有什麼辦法來自動更新的選擇2件

+1

使用'$( 「選擇」)觸發( 「變」);'改變'VAL後()'。這也會更新你的'select2()'值。 –

回答

0

找到解決方案(只是文檔不能稱之爲 「更新」):

$('#myselect').val('a').trigger('change'); 
相關問題