我如何使第一个选项的选择与jQuery?

<select id="target">
  <option value="1">...</option>
  <option value="2">...</option>
</select>

当前回答

$("#target")[0].selectedIndex = 0;

其他回答

在James Lee Baker的回复后面,我更喜欢这个解决方案,因为它消除了对浏览器支持的依赖:first:selected…

$('#target').children().prop('selected', false);
$($('#target').children()[0]).prop('selected', 'selected');

使用jQuery和ECMAScript 6检查这个最佳方法:

$('select').each((i, item) => {
  var $item = $(item);
  $item.val($item.find('option:first').val()); 
});

对我来说,它只在我添加以下代码时起作用:

.change ();

对我来说,它只在我添加以下代码时起作用: 因为我想要“重置”表单,也就是说,选择表单的所有选择的所有第一个选项,我使用了以下代码:

$(“形式”);(“选择”). each(函数(){ (美元)。瓦尔($(“选择选项:第一”).val ()); (美元).change (); });

对我来说,只有当我添加下面这行代码时,它才能工作

$('#target').val($('#target').find("option:first").val());
$("#target")[0].selectedIndex = 0;