【问题标题】:get all form inputs that are not in a particular div获取不在特定 div 中的所有表单输入
【发布时间】:2016-03-05 14:17:36
【问题描述】:

我有这样的表格

<form action="" method="post">
<div>
    <input type="text" name="input1" value="">
    <select name="my_list">
        <option value="1">1</option>
        <option value="2">2</option> 
    </select>
    <textarea name="details"></textarea>
</div>
<div class="excluded">
    <input type="text" name="input2" value="">
    <input type="text" name="input2" value="">
    <input type="text" name="input3" value="">
    <textarea name="details2"></textarea>
</div>
</form>

我想serialize 我表单的所有输入,但我想排除 div 中的输入,类 excluded。

我尝试了类似的方法,但没有成功:

$('form').find(':input').not('.excluded').serialize();

请问有谁知道我如何获取所有不在 div 中的表单输入,类 excluded?

谢谢

【问题讨论】:

  • 我想代替.not('.excluded')试试.not('.excluded :input')

标签: javascript jquery html css css-selectors


【解决方案1】:

在not() 中,您应该使用.excluded :input 而不是.excluded。

$('form :input').not('.excluded :input').serialize();

【讨论】:

    【解决方案2】:

    首先基于div 进行过滤,然后从过滤结果中获取所有输入 类似的东西

    $('form').find("div").not('.excluded').find(':input').serialize();
    

    【讨论】:

      【解决方案3】:

      使用CSS :not

      $('form div:not(.excluded) input').serialize();
      

      alert($('form div:not(.excluded) input').length);
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <form action="" method="post">
      <div>
          <input type="text" name="input1" value="">
          <select name="my_list">
              <option value="1">1</option>
              <option value="2">2</option> 
          </select>
          <textarea name="details"></textarea>
      </div>
      <div class="excluded">
          <input type="text" name="input2" value="">
          <input type="text" name="input2" value="">
          <input type="text" name="input3" value="">
          <textarea name="details2"></textarea>
      </div>
      </form>

      【讨论】:

      • 保留:input,以便包含select
      猜你喜欢
      • 2011-05-16
      • 1970-01-01
      • 2017-02-12
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多