简体   繁体   中英

jQuery adding option to select box with selected arbitrate

I want to add a option to a existing select element.

Below code works without any issue

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

Only issue is that i cant make it selected.

Can someone tell me how to do this.

 $('#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> 

Add selected

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM