【问题标题】:How to iterate over table for getting values of checkbox with checked state?如何遍历表以获取具有选中状态的复选框的值?
【发布时间】:2019-10-17 18:59:52
【问题描述】:

我必须访问并获取输入检查状态的行的值。

<table>
<thead>
<tr> <th>#</th> <th>Value 1</th> <th>Value 2</th> <th>Value 3</th> </tr>
</thead>
<tbody>
<tr>
  <td><input type='checkbox' name='1'></td> <td>1</td> <td>1</td> <td>1 </td>
</tr>
<tr>
  <td><input type='checkbox' name='2'></td> <td>2</td> <td>2</td> <td>2 </td>
</tr>
<tr>
  <td><input type='checkbox' name='3'></td> <td>3</td> <td>3</td> <td>3 </td>
</tr>
<tr>
  <td><input type='checkbox' name='4'></td> <td>4</td> <td>4</td> <td>4 </td>
</tr>
</tbody>

假设我检查了checkbox 1 and 2 的前两行,我尝试只访问这些行的值。

发生了什么:或者正在访问所有行的值,或者当我选择了 2 行时,只显示了一个。

$("table tbody tr input:checkbox:checked").each(function(){
        console.log($("table tbody tr").html());
     }); 

【问题讨论】:

    标签: jquery html-table iteration


    【解决方案1】:

    您需要将.closest()$(this) 一起使用

    $("table tbody tr input:checkbox:checked").each(function(){
       console.log($(this).closest("tr").html());
    });
    

    工作样本:-

    $("table tbody tr input:checkbox:checked").each(function(){
       console.log($(this).closest("tr").html());
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table>
      <thead>
        <tr>
          <th>#</th>
          <th>Value 1</th>
          <th>Value 2</th>
          <th>Value 3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><input type='checkbox' name='1' checked></td>
          <td>1</td>
          <td>1</td>
          <td>1</td>
        </tr>
        <tr>
          <td><input type='checkbox' name='2' checked></td>
          <td>2</td>
          <td>2</td>
          <td>2</td>
        </tr>
        <tr>
          <td><input type='checkbox' name='3'></td>
          <td>3</td>
          <td>3</td>
          <td>3</td>
        </tr>
        <tr>
          <td><input type='checkbox' name='4'></td>
          <td>4</td>
          <td>4</td>
          <td>4</td>
        </tr>
      </tbody>
    </table>

    或者你需要使用.parents()$(this)

    $("table tbody tr input:checkbox:checked").each(function(){
       console.log($(this).parents("tr").html());
    });
    

    工作样本:-

    $("table tbody tr input:checkbox:checked").each(function(){
       console.log($(this).parents("tr").html());
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table>
      <thead>
        <tr>
          <th>#</th>
          <th>Value 1</th>
          <th>Value 2</th>
          <th>Value 3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td><input type='checkbox' name='1' checked></td>
          <td>1</td>
          <td>1</td>
          <td>1</td>
        </tr>
        <tr>
          <td><input type='checkbox' name='2' checked></td>
          <td>2</td>
          <td>2</td>
          <td>2</td>
        </tr>
        <tr>
          <td><input type='checkbox' name='3'></td>
          <td>3</td>
          <td>3</td>
          <td>3</td>
        </tr>
        <tr>
          <td><input type='checkbox' name='4'></td>
          <td>4</td>
          <td>4</td>
          <td>4</td>
        </tr>
      </tbody>
    </table>

    【讨论】:

    • 是否可以将此值保存在数组中并发送到另一个页面?
    • @ThiagoL 是的。但为此,您必须提出新问题。这里很难说。您可以创建一个数组对象,遍历每个检查的 tr 并获取每个 td 值。将这些值分配给数组对象并通过 ajax 请求将其发送到另一个页面
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多