如何使用 jquery 选择所有输入,不是提交、重置或按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9320819/
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 use jquery to select all inputs, that are not submit, reset or button?
提问by frequent
I'm trying to select all input elements except input type="submit/reset/button
我正在尝试选择除 input type="submit/reset/button 之外的所有输入元素
I have tried to make a selector like this:
我试图制作一个这样的选择器:
inputSelector = 'input:not(input[type=button], input[type=submit], input[type=reset]), textarea, select';
But this does not work, because the submit buttons always make into the final selection.
但这不起作用,因为提交按钮总是进入最终选择。
Any idea what's wrong with the above.
知道上面有什么问题。
Thanks!
谢谢!
回答by Michael Robinson
Try modifying your selector to chain .not(...)
like:
尝试修改您的选择器以链接.not(...)
如下:
var inputs = $('input, textarea, select')
.not(':input[type=button], :input[type=submit], :input[type=reset]');
$(inputs).each(function() {
console.log(this.type);
});
This makes it (arguably) easier to read, and should work how you expect.
这使它(可以说)更容易阅读,并且应该按照您的预期工作。
回答by Sean Kendle
This answer is arguably harder to read than Michael Robinson's, but I find it more succinct. Rather than run the not()
function, you can select for it using :input
to gather all form elements, then filter with the :not()
pseudo class for each unwanted input type.
这个答案可以说比迈克尔罗宾逊的更难阅读,但我觉得它更简洁。not()
您可以选择用于:input
收集所有表单元素,然后使用:not()
伪类过滤每个不需要的输入类型,而不是运行该函数。
From jQuery's API documentation (https://api.jquery.com/input-selector/):
来自 jQuery 的 API 文档(https://api.jquery.com/input-selector/):
The
:input
selector basically selects all form controls.
所述
:input
选择器基本上选择所有表单控件。
The "CSS"-like selector version, the :not()
psuedo-element selectors are separate, and can be used in CSS 3, too:
类似“CSS”的选择器版本,:not()
伪元素选择器是独立的,也可以在 CSS 3 中使用:
var inputs = $("#theForm").find(":input:not([type=button]):not([type=submit]):not([type=reset])");