Jquery 选择除隐藏类型之外的所有输入

Jquery select all Input except type hidden

本文关键字:输入 选择 隐藏 类型 Jquery      更新时间:2023-09-26

我在jquery中选择所有输入,如下所示:

$('input').each(function() {
...
});

但是是否可以创建一个例外,例如说选择所有输入,隐藏输入类型除外。

我知道我可以选择我需要的特定类型的输入,但我认为这看起来并不那么好

编辑:抱歉做了错误的例子,我的意思是这样的:

document.forms["product"].getElementsByTagName("input");

隐藏除外

试试这个:你可以用:not [type="hidden"]

$(function(){
  $('input:not([type="hidden"])').each(function(){
    alert($(this).val());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input value="1">
<input type="radio" value="2">
<input type="hidden" value="3">
<input type="checkbox" value="4">

有多种方法可以使用。我更愿意使用:

$('input:visible').each();

注意::visible不会选择带有type="hidden"input

或者您可以使用更长的方法:

$('input:not([type="hidden"]').each();

$('input').not('[type="hidden"]').each();
API 参考:可见选择器、非选择器、

非选择器、不

自从您的更新以来

我的意思是这样的:

document.forms["product"].getElementsByTagName("input");

隐藏除外

尝试:

document.forms['product'].querySelectorAll('input:not([type="hidden"])')

使用.not选择器:input:hidden排除hidden元素的方法。

.not 将从匹配的元素集中删除元素

$('input').not(':input:hidden').each(function() {
...
});

使用这个:

$('input:not[type=hidden]').each(function(){
});