使用复选框从表中获取所选行值的问题

时间:2014-11-13 02:35:09

标签: javascript jquery

Demo

您能否请看一下上面的演示,让我知道为什么我无法从表中获取所选(已检查)行的值?我有这样的代码:

$("#btn").on("click", function () {
    var checkedRows = [];
    $("#tbody tr").each(function () {

        if ($(this).find("input").is(":checked")) {
            checkedRows.push($(this).find("td:eq(1)").html());
        }
        // console.log(checkedRows);    
    });

        var result = [];

    $.each($("td:eq()"), function (idx, item) {
        if (checkedRows.indexOf(idx> -1)){ result.push(item);}
    });

    if (result.length < 2) {
        alert("Please Select 2 to 4 Models");
    }
    if (result.length > 4) {
        alert("Please Select 2 to 4 Models");
    } else {
        console.log(result);
    }
    });

和HTML as:

<table border="1" width="100%">
        <thead>
                <tr>
                    <td><input type='checkbox' /></td>
                        <td>Row 1, cell 2</td>
                        <td>Row 2, cell 3</td>
                </tr>
        </thead>
        <tbody>
                <tr>
                        <td><input type='checkbox' /></td>
                        <td>Row 2, cell 2</td>
                        <td>Row 2, cell 3</td>
                </tr>
                <tr>
                        <td><input type='checkbox' /></td>
                        <td>Row 3, cell 2</td>
                        <td>Row 3, cell 3</td>
                </tr>
                <tr>
                        <td><input type='checkbox' /></td>
                        <td>Row 4, cell 2</td>
                        <td>Row 4, cell 3</td>
                     <tr>
                        <td><input type='checkbox' /></td>
                        <td>Row 4, cell 2</td>
                        <td>Row 4, cell 3</td>
                </tr>
                </tr>
        </tbody>
</table>
<input id="btn" type="button" label="button" value="get rows" />

谢谢,

  

更新

result = [
           [' Row 1, cell 2 ' , ' Row 2, cell 3 '],
           [' Row 2, cell 2 ' , ' Row 2, cell 3 '],
           [' Row 1, cell 2 ' , ' Row 2, cell 3 ']
        ]

2 个答案:

答案 0 :(得分:2)

获取所有已检查行的第二列html非常简单; .map()让它变得非常简单:

    $('#btn').on('click', function() {
        var checkedRows = $(':checkbox:checked')
        .closest('tr').find('td:eq(1)').map(function() { 
            return $(this).html(); 
         }).get();
         console.log( checkedRows );
    });

&#13;
&#13;
    $('#btn').on('click', function() {
        var checkedRows = $(':checkbox:checked')
        .closest('tr').find('td:eq(1)').map(function() { 
            return $(this).html(); 
         }).get();
        
        //Output --- just for demo
        $('pre.out').text( JSON.stringify( checkedRows ) );
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table border="1" width="100%">
        <thead>
                <tr>
                    <td><input type='checkbox' /></td>
                        <td>Row 1, cell 2</td>
                        <td>Row 2, cell 3</td>
                </tr>
        </thead>
        <tbody>
                <tr>
                        <td><input type='checkbox' /></td>
                        <td>Row 2, cell 2</td>
                        <td>Row 2, cell 3</td>
                </tr>
                <tr>
                        <td><input type='checkbox' /></td>
                        <td>Row 3, cell 2</td>
                        <td>Row 3, cell 3</td>
                </tr>
                <tr>
                        <td><input type='checkbox' /></td>
                        <td>Row 4, cell 2</td>
                        <td>Row 4, cell 3</td>
                     <tr>
                        <td><input type='checkbox' /></td>
                        <td>Row 4, cell 2</td>
                        <td>Row 4, cell 3</td>
                </tr>
                </tr>
        </tbody>
</table>
<input id="btn" type="button" label="button" value="get rows" />

<pre class="out"></pre>
&#13;
&#13;
&#13;

<强>更新

要根据此问题的评论中所示生成数组数组,请输入以下代码:

$('#btn').on('click', function() {
    var checkedRows = [];
    $(':checkbox:checked').closest('tr').each(function() {
        checkedRows.push(
          $(this).find('td:gt(0)').map(function() {
              return $(this).html();
          }).get()
        ); 
     });

    console.log( checkedRows );
});

&#13;
&#13;
    $('#btn').on('click', function() {
        var checkedRows = [];
        $(':checkbox:checked').closest('tr').each(function() {
            checkedRows.push(
              $(this).find('td:gt(0)').map(function() {
                  return $(this).html();
              }).get()
            ); 
         });
        
        //Output --- just for demo
        $('pre.out').text( JSON.stringify( checkedRows ) );
    });
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table border="1" width="100%">
        <thead>
                <tr>
                    <td><input type='checkbox' /></td>
                        <td>Row 1, cell 2</td>
                        <td>Row 2, cell 3</td>
                </tr>
        </thead>
        <tbody>
                <tr>
                        <td><input type='checkbox' /></td>
                        <td>Row 2, cell 2</td>
                        <td>Row 2, cell 3</td>
                </tr>
                <tr>
                        <td><input type='checkbox' /></td>
                        <td>Row 3, cell 2</td>
                        <td>Row 3, cell 3</td>
                </tr>
                <tr>
                        <td><input type='checkbox' /></td>
                        <td>Row 4, cell 2</td>
                        <td>Row 4, cell 3</td>
                     <tr>
                        <td><input type='checkbox' /></td>
                        <td>Row 4, cell 2</td>
                        <td>Row 4, cell 3</td>
                </tr>
                </tr>
        </tbody>
</table>
<input id="btn" type="button" label="button" value="get rows" />

<pre class="out"></pre>
&#13;
&#13;
&#13;

输出 - 选中所有复选框。

[
    ["Row 1, cell 2","Row 2, cell 3"],
    ["Row 2, cell 2","Row 2, cell 3"],
    ["Row 3, cell 2","Row 3, cell 3"],
    ["Row 4, cell 2","Row 4, cell 3"],
    ["Row 4, cell 2","Row 4, cell 3"]
]

答案 1 :(得分:1)

$("#tbody tr")应为$("tbody tr"),因为tbody不是ID。还建议您在if条件(input type)中添加input[type=checkbox]

$("#btn").on("click", function () {
    var checkedRows = [];
    $("tbody tr").each(function () {
        if ($(this).find("input[type=checkbox]").is(":checked")) {
            checkedRows.push($(this).find("td:eq(1)").html());
        }
    });
    console.log(checkedRows);
    //......
});