2016-09-21 181 views
1

我想添加一個選項到現有的select元素。jQuery將選項添加到選擇的仲裁選擇框

下面的代碼工作沒有任何問題

$('#selectStory').append($('<option>', { 
    value: 1, 
    text: 'Red' 
})); 

唯一的問題是我不能讓它選擇。

有人可以告訴我如何做到這一點。

+0

我看不到它的任何問題。請參閱https://jsfiddle.net/ausejzbv/ – Mohammad

+0

@Mhamhammad OP想要設置所選的附加選項 – guradio

回答

2

$('#selectStory').append($('<option>', { 
 
    value: 1, 
 
    text: 'Red', 
 
    selected: 'selected' 
 
}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<select id='selectStory'> 
 
    <option></option> 
 
</select>

添加selected