我如何使第一个选项的选择与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>
当前回答
使用jQuery和ECMAScript 6检查这个最佳方法:
$('select').each((i, item) => {
var $item = $(item);
$item.val($item.find('option:first').val());
});
其他回答
$("#target")[0].selectedIndex = 0;
Use:
$("#selectbox option:first").val()
请在这个JSFiddle中找到工作简单。
$("#target").val($("#target option:first").val());
虽然每个函数可能都不是必需的…
$('select').each(function(){
$(this).find('option:first').prop('selected', 'selected');
});
对我有用。
在James Lee Baker的回复后面,我更喜欢这个解决方案,因为它消除了对浏览器支持的依赖:first:selected…
$('#target').children().prop('selected', false);
$($('#target').children()[0]).prop('selected', 'selected');