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

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

当前回答

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

.change ();

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

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

其他回答

它只适用于我在最后使用触发器('change'),就像这样:

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

虽然每个函数可能都不是必需的…

$('select').each(function(){
    $(this).find('option:first').prop('selected', 'selected');
});

对我有用。

$('#newType option:first').prop('selected', true);
$("#target")[0].selectedIndex = 0;

以下是我的做法:

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