【问题标题】:Jquery select all Input except type hiddenJquery选择除了隐藏类型之外的所有输入
【发布时间】:2016-08-14 06:09:54
【问题描述】:

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

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

但是是否可以创建一个异常,例如选择所有输入,但输入类型为隐藏。

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

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

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

除了隐藏

【问题讨论】:

    标签: javascript jquery exception select input


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      有多种使用方法。我更喜欢使用:

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

      注意: :visible 不选择 inputtype="hidden"

      或者你可以使用更长一点的方法:

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

      或者,

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

      API 参考:visible-selectornot-selectornot

      【讨论】:

        【解决方案3】:

        试试这个:你可以使用: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">

        【讨论】:

          【解决方案4】:

          使用这个:

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

          【讨论】:

            【解决方案5】:

            自从你更新

            我的意思是这样的:

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

            除了隐藏

            尝试:

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

            【讨论】:

            • 所以我不能使用 getElementsByTagName?
            • 你可以,但这并没有例外。然后,您需要检查结果以检查类型。
            • 如果您使用的是 jQuery,请使用 @BhushanKawadkar 回答。
            • 所以querySelectorAll和getElementsByTagName基本一样?
            • 在什么意义上?作为一个 javascript 函数而不是 jQuery 函数,是的。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-11-28
            • 2023-03-13
            • 2010-11-22
            • 1970-01-01
            • 1970-01-01
            • 2018-05-23
            相关资源
            最近更新 更多