如何使用Javascript读取被选中复选框所在行的元素

3

这是我的问题,我有一个使用JavaScript动态创建的表格,结果如下:

<table>
<tbody id="bodyDepartment">
            <tr><td class="Depa" style="width:30px;" align="center"><input id="Depa" name="Depa" type="checkbox" value="1" style="width:30px;"/> </td>
                <td>value 1.1</td>
                <td>value 1.2</td>
                <td>value 1.3</td>
            </tr>
            <tr><td class="Depa" style="width:30px;" align="center"><input id="Depa" name="Depa" type="checkbox" value="1" style="width:30px;"/> </td>
                <td>value 2.1</td>
                <td>value 2.2</td>
                <td>value 2.3</td>
            </tr>
</tbody>
</table>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

我希望您能够获取所选行中的所有元素,我尝试使用以下JavaScript代码获取元素:
function Click(){
if ($("input:checkbox[name='Depa']:checked")) {
    var id, num, piso
    $("input:checkbox[name='Depa']:checked").parents("tr").each(function (index) {
      $("input:checkbox[name='Depa']:checked").parents("tr").children("td").each(function (index2) {
        switch (index2) {
        case 1:
            id = $(this).text();
            alert(id);
            break;
        case 2:
            num= $(this).text();
            alert(num);
            break;
        case 3:
            piso = $(this).text();
            alert(piso);
            break;

         }
      })
    })
}

但是我只能获得最后选择的 "n" 个元素,其中 "n" 是所选元素的数量。请问有人可以告诉我问题在哪里以及如何解决。

这是我的问题演示:http://jsfiddle.net/ZPxqJ/180/

2个回答

1

尝试:

function getDetails(){
    var checkboxes = $("input:checkbox[name='Depa']:checked");
    $(checkboxes).each(function(){
        var arr = $(this).closest('tr').find('td:not(.Depa)');
        $(arr).each(function(){
            console.log(this.innerHTML);
        });
    });
}

0

试试这个,

$("#RegBtnReg").click(Click);

function Click() {
    var id, num, piso
    $("input:checkbox[name='Depa']:checked").parents("tr").each(function(index) {

        $(this).children("td").each(function(index2) {
            switch (index2) {
            case 1:
                id = $(this).text();
                alert(id);
                break;
            case 2:
                num = $(this).text();
                alert(num);
                break;
            case 3:
                piso = $(this).text();
                alert(piso);
                break;

            }
        })
    })
}​

演示:http://jsfiddle.net/ZPxqJ/181/

嗨,谢谢,但是读取最后一个是否有任何方法可以按顺序阅读它? - David A

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接