2016-08-09 47 views

回答

1
$('#fieldId').select2("val",$('#fieldId option:eq(1)').val()); 
/*By using eq(), the index number should be calculated from 0*/ 

$('#fieldId').select2("val",$('#fieldId option:nth-child(2)').val()); 
/*By using nth-child(), the index number should be calculated from 1*/ 

這可能是有用的

+0

你的答案是correc but.already using this,並在評論中提到了這一點。無論如何接受你的答案來幫助別人 – Ponnarasu

0

如果你指的是select下拉菜單,然後這應該解決您的問題

<select> 
    <option>1st item</option> 
    <option selected>2nd item</option> 
    <option>3rd item</option> 
</select> 

編輯:可以實現,像這樣

$('select option:nth-child(2)').attr('selected', true); 
+0

在ajax響應中加載選項列表時,我已按照您的說法選擇了該選項。這工作得很好。有沒有其他方式像'$(「#fieldId」)。prop(「selectedIndex」,1); '使用JQuery? – Ponnarasu

+0

http://stackoverflow.com/questions/1280499/jquery-set-select-index – Cobote

+0

在這裏,我編輯了我的答案@Pannarasu。請不要忘記投票並將其標記爲答案 –

相關問題