[英]How to append text to selected item in select2?
我正在嘗試編輯select2的選定值。 所以我做了這個簡單的代碼:
HTML
<select id="sel" style="width: 100%">
<option value="1">Hello</option>
<option value="2">Friends</option>
<option value="3">Stackoverflow</option>
</select>
<br><br>
<button id="addok">Add OK</button>
JS
$(document).ready(function () {
$("#sel").select2();
$("#addok").click(function() {
actual_value = $("#sel").find(':selected').text();
if (actual_value.indexOf(" OK") > -1){
return
}
newtext = actual_value + " OK";
// this works.
$("#sel").find(':selected').text(newtext);
// this does not works.
$('#s2id_sel').find('.select2-choosen').text(newtext);
});
});
如您所見,按下按鈕后,看起來沒有任何變化,但是如果您打開下拉列表,則在當前項目中成功添加了OK
。
當我嘗試使用select2-choosen
css類修改div中的實際值時,會出現問題。 我不能通過這種方式訪問它。
你有任何想法,建議或提示嗎?
在select上觸發change()
將顯示附加的ok,你需要觸發change()
沒有id為s2id_sel
元素和class .select2-choosen
to last statement不會改變文本。
$("#sel").find(':selected').text(newtext).change();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.