我想連接optgroup,但選擇的值。 在jsFiddle當選擇是簡單的選擇它正常工作。 當選擇São Paulo
值正確的結果:Sudeste - 聖保羅 但是,當我改變選擇到多個洗牌周圍。Optgroup + value multiple select with selected2?
看看jsFiddle。
我想連接optgroup,但選擇的值。 在jsFiddle當選擇是簡單的選擇它正常工作。 當選擇São Paulo
值正確的結果:Sudeste - 聖保羅 但是,當我改變選擇到多個洗牌周圍。Optgroup + value multiple select with selected2?
看看jsFiddle。
的原因,您的format(item)
功能沒有得到所選項目的父optgroup
的是,該功能運行時,該option
實際上不選擇呢。此時,select2插件未標記在底層select
標記中選擇的option
。
如果您調試format(item)
,您可以看到$('#estados').find(':selected')
是該函數第一次運行時的空數組。
相反,您需要做的是從item
參數中獲取option
元素,然後在查詢中使用它來獲取optgroup
。
function format(item) {
// opt = $('#estados').find(':selected');
// sel = opt.text();
// og = opt.closest('optgroup').attr('label');
var og = $(item.element[0]).closest('optgroup').attr('label');
return og+'-'+item.text;
}
原諒我的英語,我真的是我想要的。 – cura
請更好地解釋是什麼問題?你的預期結果是什麼?這是令人失望的地方? – Athafoud
@Athafoud編輯。 – cura