我如何使第一个选项的选择与jQuery?
<select id="target">
<option value="1">...</option>
<option value="2">...</option>
</select>
我如何使第一个选项的选择与jQuery?
<select id="target">
<option value="1">...</option>
<option value="2">...</option>
</select>
当前回答
// remove "selected" from any options that might already be selected
$('#target option[selected="selected"]').each(
function() {
$(this).removeAttr('selected');
}
);
// mark the first option as selected
$("#target option:first").attr('selected','selected');
其他回答
$("#target").val($("#target option:first").val());
在James Lee Baker的回复后面,我更喜欢这个解决方案,因为它消除了对浏览器支持的依赖:first:selected…
$('#target').children().prop('selected', false);
$($('#target').children()[0]).prop('selected', 'selected');
就像这样简单:
$('#target option:first').prop('selected', true);
var firstItem = $(“感兴趣类型”)。(“selectedIndex”道具,0)。瓦尔();
控制台日志(firstItem)。
Use:
$("#selectbox option:first").val()
请在这个JSFiddle中找到工作简单。