【问题标题】:Iterating through all selected checkboxes and get the value of input boxes in jquery [duplicate]遍历所有选中的复选框并获取jquery中输入框的值[重复]
【发布时间】:2016-12-31 01:31:54
【问题描述】:

我有这段代码,在表格行中有复选框和输入框,我想获取选中复选框的所有输入框的值

这是html

<table>
<tr>
<td><input type="checkbox" name="new" value="37"></td>
<td><input type="text" name="new" id="quantity" class="37"></td>
</tr>

 <tr>
 <td><input type="checkbox" name="new" value="38"></td>
 <td><input type="text" name="new" id="quantity" class="38"></td>
 </tr>
  .....#this continue
   </table>

注意输入框的类和checkboxes的值是一样的

目前正在使用此代码检查所有选中的复选框

    var marked = new Array();
    var k = 0;
    $('input:checked').each(function(index,value) {

        marked[k] = value;
        k++;
        alert(k);
    });        
    alert($('input[name="new"]:checked').serialize());

如何更改它以返回输入框的值

【问题讨论】:

    标签: javascript jquery html checkbox


    【解决方案1】:

    试试这个:

    $(document).ready(function(){
            $( "input[type=checkbox]" ).each(function(){
                if($(this).is(':checked'))
                {
                    var value = $(this).closest('tr').find($( "input[type=text]" )).val();
                    alert(value);
                }
            });
        });
    

    【讨论】:

    • 这会遍历所有这些,但我如何序列化 .each 函数之外的值
    • 将它们推入一个数组,然后相应地使用它
    【解决方案2】:

    var arr=[];
    
    $(':checkbox:checked').each(function(){
    
    arr.push($(this).parent().next().find('input').val())
    //arr.push($(this).val())
    
    })
    
    console.log(arr)
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
    <tr>
      <td>
        <input type="checkbox" name="new" value="37" checked>
      </td>
      <td>
        <input type="text" name="new" id="quantity" class="37" value='asd'>
      </td>
    </tr>
    
    <tr>
      <td>
        <input type="checkbox" name="new" value="38" checked>
      </td>
      <td>
        <input type="text" name="new" id="quantity" class="38" value='qwe'>
      </td>
    </tr>
    </table>

    试试这个方法

    【讨论】:

      猜你喜欢
      • 2020-01-16
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多