Javascript jQuery - 检查输入是否不是选择字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4853254/
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
jQuery - check if an input is not a select field
提问by Alex
How can I find out if an input field is anything other than a select
?
如何确定输入字段是否是 a 以外的任何内容select
?
I tried with if($(el).not("select"))
and I get the selects too...
我试过了if($(el).not("select"))
,我也得到了选择......
回答by Darin Dimitrov
if(!$(el).is("select")) {
// the input field is not a select
}
回答by gor
回答by Syl
if ($(el)[0].nodeName != 'select')
回答by SLaks
The .not()
method returns a new jQuery object with everything from the original object that doesn't match the selector.
You can't just use it in an if
statement like that.
该.not()
方法返回一个新的 jQuery 对象,其中包含原始对象中与选择器不匹配的所有内容。
你不能只在这样的if
声明中使用它。
Instead, you use the .is
method, which checks whether the element matches a selector and returns a boolean.
相反,您使用该.is
方法,该方法检查元素是否与选择器匹配并返回一个布尔值。
if (!$(el).is('select'))
回答by Barrie Reader
What about giving the <select>
tags a class:
给<select>
标签一个类怎么样:
$(el + ":not('.select')")
回答by Diodeus - James MacFarlane
if($(el).selectedIndex)
If it has a SelectedIndex property, it's a <SELECT>
如果它有一个 SelectedIndex 属性,它是一个<SELECT>