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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:26:04  来源:igfitidea点击:

jQuery - check if an input is not a select field

javascriptjqueryformsjquery-selectors

提问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

$(el).not("select")gives you array. Array is always gives truein boolean expressions. But after you apply not, this array of elements won't contain selects. See working example.

$(el).not("select")给你数组。数组总是在布尔表达式中给出true。但是在你不申请之后,这个元素数组将不包含选择。请参阅工作示例

回答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 ifstatement like that.

.not()方法返回一个新的 jQuery 对象,其中包含原始对象中与选择器不匹配的所有内容。
你不能只在这样的if声明中使用它。

Instead, you use the .ismethod, 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>