如何制作< select>的第一个选项用jQuery选中
题
如何使用jQuery创建第一个选项?
<select id="target">
<option value="1">...</option>
<option value="2">...</option>
</select>
解决方案
$("#target").val($("#target option:first").val());
其他提示
你可以试试这个
$("#target").prop("selectedIndex", 0);
// 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").val($("#target option:first").val());
如果第一个选项值为空,则无法在Chrome和Safari中使用。
我更喜欢
$("#target option:first").attr('selected','selected');
因为它可以在所有浏览器中使用。
更改选择输入的值或调整所选属性可能会覆盖DOM元素的默认selectedOptions属性,从而导致元素在调用了重置事件的表单中无法正确重置。
使用jQuery的prop方法清除并设置所需的选项:
$("#target option:selected").prop("selected", false);
$("#target option:first").prop("selected", "selected");
$("#target")[0].selectedIndex = 0;
我认为我发现有关最高投票答案的一个微妙之处在于即使他们正确地更改了所选值,他们也不会更新用户看到的元素(仅在他们点击时)小部件他们会看到更新元素旁边的支票。)
将.change()调用链接到最后也会更新UI小部件。
$("#target").val($("#target option:first").val()).change();
(请注意,我在使用jQuery Mobile和Chrome桌面上的一个方框时注意到了这一点,所以在任何地方都可能不是这样)。
如果您已禁用选项,则可以添加 not([disabled])以防止选择它们,结果如下:
$("#target option:not([disabled]):first").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;
});
$('#newType option:first').prop('selected', true);
这样简单:
$('#target option:first').prop('selected', true);
我发现如果已经选择了属性,那么只选择设置的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');
虽然每个功能可能都没有必要......
$('select').each(function(){
$(this).find('option:first').prop('selected', 'selected');
});
适合我。
它最终只对我使用触发器('更改'),如下所示:
$("#target option:first").attr('selected','selected').trigger('change');
如果您打算将第一个选项用作默认选项,例如
<select>
<option value="">Please select an option below</option>
...
然后你可以使用:
$('select').val('');
很简单。
在James Lee Baker的回复背后,我更喜欢这个解决方案,因为它消除了对浏览器支持的依赖:首先:选择......
$('#target').children().prop('selected', false);
$($('#target').children()[0]).prop('selected', 'selected');
对我而言,只有在我添加以下代码时才有效:
<代码> .change(); 代码>
对我而言,只有在我添加以下代码时才有效: 因为我想“重置”表单,即选择表单所有选择的所有第一个选项,我使用以下代码:
<代码> $( '形式')。发现( '选择')。每个(函数(){ $(this).val($(&quot; select option:first&quot;)。val()); $(本).change(); }); 代码>
$("#target").val(null);
在chrome中工作正常
如果要存储select元素的jQuery对象:
var jQuerySelectObject = $("...");
...
jQuerySelectObject.val(jQuerySelectObject.children().eq(0).val());
使用jQuery和 ECMAScript&nbsp; 6 检查这种最佳方法:
$('select').each((i, item) => {
var $item = $(item);
$item.val($item.find('option:first').val());
});
对我来说,只有在我添加以下代码行
时才有效$('#target').val($('#target').find("option:first").val());
您可以使用下拉列表
选择任何选项。
// '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);
使用:
alert($( "#target option:first" ).text());