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

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

当前回答

$("#target").val(null);

镀铬加工得很好

其他回答

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

你可以试试这个

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

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

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

我发现,如果已经有一个选定的属性,只是设置attr选择不工作。我现在使用的代码将首先取消设置所选属性,然后选择第一个选项。

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

以下是我的做法:

$("#target option")
    .removeAttr('selected')
    .find(':first')     // You can also use .find('[value=MyVal]')
        .attr('selected','selected');