jQuery 如何使用jquery检查下拉列表的选定索引不是0或-1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2875153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if selected index of dropdownlist is not 0 or -1 using jquery
提问by Eyla
How to check if selected index of dropdownlist is not 0 or -1 using jquery?
如何使用jquery检查下拉列表的选定索引不是0或-1?
回答by BalusC
Unless you've the actual option
element to your hands, jQuery doesn't have special facilities for this. Just access the element's standard selectedIndex
attribute:
除非您拥有实际的option
元素,否则jQuery 没有特殊功能。只需访问元素的标准selectedIndex
属性:
var selectedIndex = $('#dropdownId').attr('selectedIndex');
if (selectedIndex > 0) {
// ...
}
回答by Glennular
$("select#elem").get(0).selectedIndex > 0
回答by James Drinkard
I had to check to see if selected index was greater than 0, so I used:
我必须检查所选索引是否大于 0,所以我使用了:
if($("select option:selected").index() > 0) {
alert($("select option:selected").index());
}
回答by S Pangborn
Taking a different approach from the others, jQuery can check the "value" of the option, as well.
采用与其他方法不同的方法,jQuery 也可以检查选项的“值”。
<select id="checkme">
<option value="0">0</option>
<option value="-1">-1</option>
</select>
And the jQuery:
和 jQuery:
$(document).ready( function () {
var theValue = $("#checkme").val();
alert("The value of the select is: " + theValue);
});
This way you don't have to know what index maps to what value, you just check the value of the select
, and it will tell you what option
is selected.
这样您就不必知道什么索引映射到什么值,您只需检查 的值select
,它就会告诉您option
选择了什么。