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

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

当前回答

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

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

其他回答

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

控制台日志(firstItem)。

另一种重置值(对于多个选定元素)的方法是:

$("selector").each(function(){

    /*Perform any check and validation if needed for each item */

    /*Use "this" to handle the element in javascript or "$(this)" to handle the element with jquery */

    this.selectedIndex=0;

});

如果你打算使用第一个选项作为默认的

<select>
    <option value="">Please select an option below</option>
    ...

然后你可以使用:

$('select').val('');

它很漂亮,也很简单。

Use:

$("#selectbox option:first").val()

请在这个JSFiddle中找到工作简单。

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

.change ();

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

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