How do i make the particular item selected in select box using jquery on document load
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 d开发者_如何学编程ocument 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
It should be do-able with:
$('option[value=AC]').attr('selected','selected');
Demo at: jsbin.
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/
You probably want
$('#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>
精彩评论