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

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

当前回答

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

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

其他回答

你可以试试这个

$("#target").prop("selectedIndex", 0);

Use:

alert($( "#target option:first" ).text());

您可以使用它从下拉菜单中选择任何选项。

// 'N' can by any number of option.  // e.g.,  N=1 for first option
$("#DropDownId").val($("#DropDownId option:eq(N-1)").val()); 

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

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

当你使用

$("#target").val($("#target option:first").val());

如果第一个选项值为空,这将在Chrome和Safari中不起作用。

我更喜欢

$("#target option:first").attr('selected','selected');

因为它可以在所有浏览器中工作。