Set select option 'selected', by value
I have a select
field with some options in it. Now I need to select one of those options
with jQuery. But how can I do that when I only know the value
of the option
that must be selected?
I have the following HTML:
<div class="id_100">
<select>
<option value="val1">Val 1</option>
<option value="val2">Val 2</option>
<option value="val3">Val 3</option>
</select>
</div>
I need to select the option with value val2
. How can this be done?
Here's a demo page: http://jsfiddle.net/9Stxb/
There's an easier way that doesn't require you to go into the options tag:
$("div.id_100 select").val("val2");
Check out the this jQuery method.
要选择值为'val2'的选项:
$('.id_100 option[value=val2]').attr('selected','selected');
Use the change()
event after selecting the value. From the docs:
If the field loses focus without the contents having changed, the event is not triggered. To trigger the event manually, apply .change()
without arguments:
$("#select_id").val("val2").change();
More information is at .change().
链接地址: http://www.djcxy.com/p/14696.html下一篇: 按值设置选择选项'selected'