【发布时间】:2010-11-27 17:00:15
【问题描述】:
如何使用 jQuery 选择第一个选项?
<select id="target">
<option value="1">...</option>
<option value="2">...</option>
</select>
【问题讨论】:
如何使用 jQuery 选择第一个选项?
<select id="target">
<option value="1">...</option>
<option value="2">...</option>
</select>
【问题讨论】:
$("#target").val($("#target option:first").val());
【讨论】:
// 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');
【讨论】:
each函数,应该是:$('#target option[selected="selected"]').removeAttr('selected')现在也应该与removeProp和prop一起使用。
当你使用
$("#target").val($("#target option:first").val());
如果第一个选项值为 null,这将在 Chrome 和 Safari 中不起作用。
我更喜欢
$("#target option:first").attr('selected','selected');
因为它可以在所有浏览器中运行。
【讨论】:
$("#target")[0].selectedIndex = 0;
【讨论】:
如果您禁用了选项,您可以添加 not([disabled]) 以防止选择它们,这会导致以下结果:
$("#target option:not([disabled]):first").attr('selected','selected')
【讨论】:
我发现如果已经有一个 selected 属性,那么仅设置 attr selected 是行不通的。我现在使用的代码会先取消设置 selected 属性,然后选择第一个选项。
$('#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');
【讨论】:
另一种重置值的方法(对于多个选定元素)可能是这样的:
$("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;
});
【讨论】:
$("selector"),你可以写$('#target')
如果你打算使用第一个选项作为默认选项
<select>
<option value="">Please select an option below</option>
...
那么你可以使用:
$('select').val('');
它既好又简单。
【讨论】:
更改 select 输入的值或调整 selected 属性可能会覆盖 DOM 元素的默认 selectedOptions 属性,从而导致元素在调用了 reset 事件的表单中可能无法正确重置。
使用jQuery的prop方法来清除和设置需要的选项:
$("#target option:selected").prop("selected", false);
$("#target option:first").prop("selected", "selected");
【讨论】:
我认为我发现关于投票最多的答案的一个微妙点是,即使他们正确更改了所选值,他们也不会更新用户看到的元素(仅当他们点击小部件他们会在更新的元素旁边看到一个检查)。
将 .change() 调用链接到末尾也会更新 UI 小部件。
$("#target").val($("#target option:first").val()).change();
(请注意,我在使用 jQuery Mobile 和 Chrome 桌面上的框时注意到了这一点,因此可能并非所有地方都如此)。
【讨论】:
你可以试试这个
$("#target").prop("selectedIndex", 0);
【讨论】:
$("#target").prop("selectedIndex", 0).change();
$('#newType option:first').prop('selected', true);
【讨论】:
$("#target").val(null);
在 chrome 中运行良好
【讨论】:
虽然可能不需要每个函数...
$('select').each(function(){
$(this).find('option:first').prop('selected', 'selected');
});
为我工作。
【讨论】:
如果您要存储选择元素的 jQuery 对象:
var jQuerySelectObject = $("...");
...
jQuerySelectObject.val(jQuerySelectObject.children().eq(0).val());
【讨论】:
【讨论】:
.val() 只是获取列表中第一个选项的值。正如原始问题中所问的那样,它实际上并没有选择(选择)第一个选项。
就这么简单:
$('#target option:first').prop('selected', true);
【讨论】:
用途:
alert($( "#target option:first" ).text());
【讨论】:
在 James Lee Baker 的回复中,我更喜欢这个解决方案,因为它消除了对浏览器对 :first :selected ... 支持的依赖。
$('#target').children().prop('selected', false);
$($('#target').children()[0]).prop('selected', 'selected');
【讨论】:
它只对我有用,最后使用触发器('change'),如下所示:
$("#target option:first").attr('selected','selected').trigger('change');
【讨论】:
使用带有ECMAScript 6的jQuery检查这个最佳方法:
$('select').each((i, item) => {
var $item = $(item);
$item.val($item.find('option:first').val());
});
【讨论】:
对我来说,它只有在我添加以下代码时才有效:
.change();
对我来说,它仅在我添加以下代码时才有效: 由于我想“重置”表单,即选择表单所有选择的所有第一个选项,我使用了以下代码:
$('form').find('select').each(function(){
$(this).val($("select option:first").val());
$(this).change();
});
【讨论】:
对我来说,它只有在我添加以下代码行时才有效
$('#target').val($('#target').find("option:first").val());
【讨论】:
您可以使用它从dropdown 中选择任何选项。
// 'N' can by any number of option. // e.g., N=1 for first option
$("#DropDownId").val($("#DropDownId option:eq(N-1)").val());
【讨论】:
$('select#id').val($('#id option')[index].value)
将 id 替换为特定的选择标签 ID,将 index 替换为您要选择的特定元素。
即
<select class="input-field" multiple="multiple" id="ddlState" name="ddlState">
<option value="AB">AB</option>
<option value="AK">AK</option>
<option value="AL">AL</option>
</select>
所以这里对于第一个元素选择我将使用以下代码:
$('select#ddlState').val($('#ddlState option')[0].value)
【讨论】:
这对我有用!
$("#target").prop("selectedIndex", 0);
【讨论】:
var firstItem = $("#interest-type").prop("selectedIndex", 0).val();
console.log(firstItem)
【讨论】: