[英]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 document load I want to Make item "AC" in the select box as the default selected option on loading the document 所以在文档加载时我想在选择框中将项目“AC”作为加载文档时的默认选择选项
$('#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. 所以写了这个声明,它使我的选择框完全为我的选择框填充值'AC'。 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
我如何纠正这一点,我尝试使用if语句比较'AC'的值。我没有选项的“值”作为数字
You can use .val() to set the value of the select. 您可以使用.val()来设置select的值。 This will select the option with the specified value.
这将选择具有指定值的选项。
$("#myselect").val('AC');
Example here: http://jsfiddle.net/mtd6z/ 示例: 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 ) - 我建议不要使用Javascript(除非一些非常重要的原因) -
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.