iam having the select box
<select id ="myselect">
<option value="AA">AA</option>
<option value="AB">AB</option>
<option value="AC">AC</option>
<option value="AD">AD</option>
</select>
so on document load I want to Make item "AC" in the select box as the default selected option on loading the document
$('#myselect').children('option').text('AC').attr('selected', 'selected');
so with this statement written it is making my select box to fill with values 'AC' completely for my select box. How do i correct that and i tried in using the if statement comparing the value for 'AC'.And i dont have the "Value" for option as number
You can use .val() to set the value of the select. This will select the option with the specified value.
$("#myselect").val('AC');
Example here: http://jsfiddle.net/mtd6z/
你可能想要
$('#myselect').children('option').filter(function(){ return $(this).text() == 'AC' }).attr('selected', 'selected');
I suggest not using Javascript for this (unless some very important reason ) -
It can be simply done this way
<select id ="myselect">
<option value="AA">AA</option>
<option value="AB">AB</option>
<option value="AC" selected="selected">AC</option>
<option value="AD">AD</option>
</select>
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.