2017-08-07 51 views
0

我試圖更新我的JS代碼以適應v4(由ACF使用)。Select2 v3> v4 - 以編程方式設置值問題

這曾與V3(其中defCountry是先前設置的變量):

$('#s2id_acf-field_554f18ecbf60c-input').select2("data", {id: defCountry, text: defCountry}); 

但它不與V4工作。我嘗試了這些,但無濟於事:

$('#s2id_acf-field_554f18ecbf60c-input').val(defCountry); 
$('#s2id_acf-field_554f18ecbf60c-input').val({id: defCountry, text: defCountry}); 
$('#s2id_acf-field_554f18ecbf60c').val(defCountry); 
$('#s2id_acf-field_554f18ecbf60c').val({id: defCountry, text: defCountry}); 

(既沒有觸發選項)。

回答

0

這應該工作:

$('#...').val(defCountry).trigger('change'); 

你應該叫.val()相同的元素,你上確實叫.select2(...)