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

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

当前回答

Use:

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

其他回答

// remove "selected" from any options that might already be selected
$('#target option[selected="selected"]').each(
    function() {
        $(this).removeAttr('selected');
    }
);


// mark the first option as selected
$("#target option:first").attr('selected','selected');
$("#target")[0].selectedIndex = 0;

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

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

var firstItem = $(“感兴趣类型”)。(“selectedIndex”道具,0)。瓦尔();

控制台日志(firstItem)。

更改选择输入的值或调整所选属性会覆盖DOM元素的默认selectedOptions属性,导致元素在调用了重置事件的表单中可能无法正确重置。

使用jQuery的prop方法清除和设置所需的选项:

$("#target option:selected").prop("selected", false);
$("#target option:first").prop("selected", "selected");