使用Jquery选中复选框时获取标记的属性值

时间:2014-03-03 17:31:18

标签: jquery jquery-selectors

在以下HTML中,我尝试获取所有整数的总和选中复选框时 例如,在这里,当选中两个复选框时我希望获得750和755的总和

<table summary='' id='table_csrdownloadcenter'>
    <thead>
        <tr>
            <th>text1</th>
            <th>text2</th>        
            <th>text3</th>
            <th>text4</th>
            <th>text5</th>
            <th>text6</th>
            <th>text7</th>
            <th style="display:none">hidden</th>
            <th>text8</th>
        </tr>
    </thead>
    <tbody>

        <tr id='nom_du_pdf'>
            <td class='dc-date'>1</td>
            <td class='dc-dl'>2</td>
            <td class='dc-title'>3</td>
            <td class='dc-area'>4</td>
            <td class='dc-category'>5</td>
            <td class='dc-file'>6</td>
            <td class='dc-ranking'>7</td>
            <td class='hidden_size' style="display:none">750</td>
            <td class='dc-checkbox'><input type='checkbox' name='chk"+PathFile[i]+"' ></td>
        </tr>

        <tr id='nom_du_pdf2'>
            <td class='dc-date'>1.1</td>
            <td class='dc-dl'>2.2</td>
            <td class='dc-title'>3.3</td>
            <td class='dc-area'>4.4</td>
            <td class='dc-category'>5.5</td>
            <td class='dc-file'>6.6</td>
            <td class='dc-ranking'>7.7</td>
            <td class='hidden_size' style="display:none">755</td>
            <td class='dc-checkbox'><input type='checkbox' name='chk"+PathFile[i]+"' ></td>
        </tr>
    </tbody>
</table>

这是我的尝试(不工作): 我认为第二行存在问题。我怎么能从复选框输入到“hidden_​​size”标签

jQuery(function () {
    var temp = 0;
    $("table#table_csrdownloadcenter tr td input[type=checkbox]:checked").each(){
        temp += $(this).closest('tr').find("hidden_size").Text();

})

提前感谢您的帮助

4 个答案:

答案 0 :(得分:0)

.find("hidden_size").Text()应为.find(".hidden_size").text()

例如:

$("table#table_csrdownloadcenter tr td input[type=checkbox]:checked").each(function () {
    temp += +$(this).closest('tr').find(".hidden_size").text();
})

<强> jsFiddle example

答案 1 :(得分:0)

试试这个,课程应为.hidden_size

 $(this).closest('tr').find(".hidden_size").text();

答案 2 :(得分:0)

每次勾选或勾选任何相关复选框时,这将为您计算出价值......

$("#table_csrdownloadcenter input:checkbox").on("change", function() {
    var $checked = $("#table_csrdownloadcenter input:checkbox:checked");
    var total = 0;
    $checked.each(function() {
        var value = parseInt($(this).closest("tr").find("td.hidden-size").text(), 10);
        total += value;
    });
    // do what you want with the total here
    alert(total);
});

答案 3 :(得分:0)

$('input[type="checkbox"]:checked','#table_csrdownloadcenter').each(function(){
   temp+=$(this).parent().prev('.hidden_size').text();
});