如何使用jquery选择所有非提交、重置或按钮的输入?

发布于 2025-01-06 09:17:50 字数 272 浏览 2 评论 0原文

我试图选择除 input type="submit/reset/button 之外的所有输入元素

我尝试制作这样的选择器:

inputSelector = 'input:not(input[type=button], input[type=submit], input[type=reset]), textarea, select';

但这不起作用,因为提交按钮总是进入最终选择。

知道出了什么问题吗与上述

谢谢!

I'm trying to select all input elements except 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从此见与不见 2025-01-13 09:17:50

尝试将您的选择器修改为链 .not(...) ,如下所示:

var inputs = $('input, textarea, select')
                .not(':input[type=button], :input[type=submit], :input[type=reset]');

$(inputs).each(function() {
    console.log(this.type);
});

这使其(可以说)更易于阅读,并且应该按照您的预期工作。

Try modifying your selector to chain .not(...) like:

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.

倾城泪 2025-01-13 09:17:50

这个答案可以说比迈克尔·罗宾逊的更难阅读,但我发现它更简洁。您可以使用 :input 来选择它来收集所有表单元素,然后使用 :not()not() 函数code> 每个不需要的输入类型的选择器。

来自 jQuery 的 API 文档 (https://api.jquery.com/input-selector/ ):

:input 选择器基本上选择所有表单控件。

类似“CSS”的选择器版本,:not() 选择器是独立的,也可以在 CSS 3 中使用:

var inputs = $("#theForm").find("input:not([type=button]):not([type=submit]):not([type=reset])");

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() selector for each unwanted input type.

From jQuery's API documentation (https://api.jquery.com/input-selector/):

The :input selector basically selects all form controls.

The "CSS"-like selector version, the :not() selectors are separate, and can be used in CSS 3, too:

var inputs = $("#theForm").find("input:not([type=button]):not([type=submit]):not([type=reset])");
戴着白色围巾的女孩 2025-01-13 09:17:50

更短、更简洁的方法:

var inputs = $(':input[type!=button]:input[type!=submit]:input[type!=reset]');

或者

$("#some-selector :input[type!=button]:input[type!=submit]:input[type!=reset]');

Even shorter, more concise approach:

var inputs = $(':input[type!=button]:input[type!=submit]:input[type!=reset]');

Or

$("#some-selector :input[type!=button]:input[type!=submit]:input[type!=reset]');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文