[英]jQuery how to select the first non-hidden select option
The following behaves differently between jQuery 1.9 and 1.10+: 以下行为在jQuery 1.9和1.10+之间有所不同:
<select id="s1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
$('#s1 option[value=1]').hide();
$('#s1').val('');
The idea behind this code is to select the first non-hidden option, after hiding some options, including the currently selected one. 该代码背后的思想是在隐藏一些选项(包括当前选中的选项)之后,选择第一个非隐藏选项。
Since jQuery 1.10+ the $('#s1').val('');
从jQuery 1.10+开始,
$('#s1').val('');
no longer selects the first non-hidden option, while .val(<some proper value for the particular select box>)
works ok. 不再选择第一个非隐藏选项,而
.val(<some proper value for the particular select box>)
正常。
Trying the following approaches does not help because both selectedIndex
and .first().val()
consider the hidden options: 尝试以下方法无济于事,因为
selectedIndex
和.first().val()
考虑了隐藏选项:
$("#s1").prop("selectedIndex", 0);
$('#s1 option').first().prop('selected', true);
Next thing that comes to mind (also suggested by C-link ) also does not work, because the :visible
selector does not work properly for select options. 想到的下一件事情(也由C-link提出 )也不起作用,因为
:visible
选择器不适用于选择选项。
$('#s1 option:visible').first().prop('selected', true);
Looking for some generic way (not depending on knowledge of what are the particular values and what options have been hidden) to achieve the same behaviour as $('#s1').val('');
寻找某种通用方法(不取决于对特定值和隐藏的选项的了解)来实现与
$('#s1').val('');
相同的行为$('#s1').val('');
in old jQuery. 在旧的jQuery中。
像这样不久:
$('#s1').find("option:not(:hidden):eq(0)");
Compiled from everybody else's answers/comments the following: 从其他人的答案/评论中汇编出以下内容:
$('#s1 option').each(function () {
if ($(this).css('display') != 'none') {
$(this).prop("selected", true);
return false;
}
});
Try this: 尝试这个:
<select id="s1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
$('#s1 option[value=1]').hide();
var firstVisibleValue = '';
$('#s1').children().each(function(){ //iterate options
//check if option is visible - if not set to display none, which is what `.hide` does.
if($(this).css('display') != 'none'){
firstVisibleValue = $(this).val();
return false; //stop iterating
}
});
$('#s1').val(firstVisibleValue);
Try this code 试试这个代码
$('#s1 option[value="1"]').hide();
$('#s1').find('option').each(function()
{
if($(this).is(':visible'))
{
$(this).attr("selected","selected");
return false;
}
});
也许这:
$('#s1').find("option:not([hidden]):eq(0)");
You can easily do this using class 您可以使用课程轻松完成此操作
$('#s1 option[value=1]').hide(); $('#s1 option[value=1]').addClass('hidden'); $('#s1 option[value=1]').removeClass('visible') $('#s1').val(''); $('#s1 .visible:first').val();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="s1"> <option class="visible" value="1">1</option> <option class="visible" value="2">2</option> <option class="visible" value="3">3</option> </select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.