查找是否在tr中选中了复选框

时间:2013-09-04 12:09:44

标签: javascript jquery html checkbox

我确定是否在父tr中选中了复选框。如果复选框被剔除,这个tr应该被滑倒。在我的代码中似乎一切都是正确的,但它不起作用。

$(document).ready(function () {
    var sum = 0;
    $('#MainContent_gvOrders > tbody > tr > td .col1').each(function () {
        if ($(this).closest('tr').has('input:not(:checked)')) {
            sum += parseInt($(this).text());
        }
    });
    $('#tp').html(sum);
});

function pageLoad(sender, args) {
    var sum = 0;
    $('#MainContent_gvOrders > tbody > tr > td .col1').each(function () {
        if ($(this).closest('tr').has('input:not(:checked)')) {
            sum += parseInt($(this).text());
        }
    });
    $('#tp').html(sum);
}

4 个答案:

答案 0 :(得分:2)

您可以使用以下代码执行此操作。

$('#MainContent_gvOrders  tr:has("input:not(:checked)")').filter(function(){
     return sum += parseInt($(this).find('td').text(), 10);
});

Demo

答案 1 :(得分:1)

只需将$(this).closest('tr').has('input:not(:checked)')替换为$(this).closest('tr').has('input:not(:checked)')[0]即可。 (最后添加[0]

问题是jquery函数总是返回一些东西( object )。所以,如果你检查if(object)它将永远是真的。

$(document).ready(function () {
    var sum = 0;
    $('#MainContent_gvOrders > tbody > tr > td .col1').each(function () {
        if ($(this).closest('tr').has('input:not(:checked)')[0]) {
            sum += parseInt($(this).text(), 10);
        }
    });
    $('#tp').html(sum);
});

答案 2 :(得分:0)

这是HTML:

    <div>
    <table class="bordered striped hovered" cellspacing="0" rules="all" border="1" id="MainContent_gvOrders" style="border-collapse:collapse;">
        <tr>
            <th scope="col">&nbsp;</th><th scope="col">Заказ</th><th scope="col">Pax</th><th scope="col">Child</th><th scope="col">PickUpTime</th><th scope="col">Скидка</th><th scope="col">Продал</th><th scope="col">Транспорт</th><th scope="col">Отмен</th><th scope="col">Причина</th><th scope="col">WhenSold</th><th scope="col">Price</th><th scope="col">Net</th><th scope="col">PriceChd</th><th scope="col">NetChd</th><th scope="col">TotalPrice</th><th scope="col">TotalNet</th>
        </tr><tr>
            <td><input type="image" src="../img/edit.png" alt="Edit" onclick="javascript:__doPostBack(&#39;ctl00$MainContent$gvOrders&#39;,&#39;Edit$0&#39;);return false;" /></td><td>Aphrodita VIP 03 Sep 2013</td><td>
                    <span id="MainContent_gvOrders_Label4_0">1</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label5_0">0</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label11_0"></span>
                </td><td>
                    <span id="MainContent_gvOrders_Label12_0">0</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label1_0">Igor</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label13_0">0</span>
                </td><td>
                    <span class="aspNetDisabled"><input id="MainContent_gvOrders_cbCancel_0" type="checkbox" name="ctl00$MainContent$gvOrders$ctl02$cbCancel" checked="checked" disabled="disabled" /></span>
                </td><td>
                    <span id="MainContent_gvOrders_Label14_0"></span>
                </td><td>
                    <span id="MainContent_gvOrders_Label6_0"></span>
                </td><td>
                    <span id="MainContent_gvOrders_Label7_0">800</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label8_0">380</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label9_0">600</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label10_0">350</span>
                </td><td class="col1">
                    <span id="MainContent_gvOrders_Label2_0" class="col1">800</span>
                </td><td class="col2">
                    <span id="MainContent_gvOrders_Label3_0" class="col2">380</span>
                </td>
        </tr><tr>
            <td><input type="image" src="../img/edit.png" alt="Edit" onclick="javascript:__doPostBack(&#39;ctl00$MainContent$gvOrders&#39;,&#39;Edit$1&#39;);return false;" /></td><td>Flying Hanuman (B) 02 Sep 2013</td><td>
                    <span id="MainContent_gvOrders_Label4_1">1</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label5_1">0</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label11_1"></span>
                </td><td>
                    <span id="MainContent_gvOrders_Label12_1">0</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label1_1">Igor</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label13_1">0</span>
                </td><td>
                    <span class="aspNetDisabled"><input id="MainContent_gvOrders_cbCancel_1" type="checkbox" name="ctl00$MainContent$gvOrders$ctl03$cbCancel" disabled="disabled" /></span>
                </td><td>
                    <span id="MainContent_gvOrders_Label14_1"></span>
                </td><td>
                    <span id="MainContent_gvOrders_Label6_1"></span>
                </td><td>
                    <span id="MainContent_gvOrders_Label7_1">3250</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label8_1">2200</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label9_1">3250</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label10_1">2200</span>
                </td><td class="col1">
                    <span id="MainContent_gvOrders_Label2_1" class="col1">3250</span>
                </td><td class="col2">
                    <span id="MainContent_gvOrders_Label3_1" class="col2">2200</span>
                </td>
        </tr><tr>
            <td><input type="image" src="../img/edit.png" alt="Edit" onclick="javascript:__doPostBack(&#39;ctl00$MainContent$gvOrders&#39;,&#39;Edit$2&#39;);return false;" /></td><td>Khao Lak 05 Sep 2013</td><td>
                    <span id="MainContent_gvOrders_Label4_2">1</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label5_2">0</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label11_2">7:15</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label12_2">0</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label1_2">Igor</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label13_2">0</span>
                </td><td>
                    <span class="aspNetDisabled"><input id="MainContent_gvOrders_cbCancel_2" type="checkbox" name="ctl00$MainContent$gvOrders$ctl04$cbCancel" disabled="disabled" /></span>
                </td><td>
                    <span id="MainContent_gvOrders_Label14_2"></span>
                </td><td>
                    <span id="MainContent_gvOrders_Label6_2"></span>
                </td><td>
                    <span id="MainContent_gvOrders_Label7_2">2700</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label8_2">1500</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label9_2">1800</span>
                </td><td>
                    <span id="MainContent_gvOrders_Label10_2">1300</span>
                </td><td class="col1">
                    <span id="MainContent_gvOrders_Label2_2" class="col1">2700</span>
                </td><td class="col2">
                    <span id="MainContent_gvOrders_Label3_2" class="col2">1500</span>
                </td>
        </tr><tr>
            <td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>
                    <span id="tp"></span>
                </td><td>
                    <span id="tn"></span>
                </td>
        </tr>
    </table>
</div>

这个标记由asp.net gridview控件生成。

答案 3 :(得分:0)

最后,我找到了解决方案。

<script type="text/javascript">
        $(document).ready(function () {
            var sum = 0;
            $('#MainContent_gvOrders  tr:has(:checkbox:not(:checked))').filter(function () {
                sum += parseInt($(this).find('td.col1').text(), 10);
                });
            $('#tp').html(sum);
        });
        function pageLoad(sender, args) {
            var sum = 0;
            $('#MainContent_gvOrders  tr:has(:checkbox:not(:checked))').filter(function () {
               sum += parseInt($(this).find('td.col1').text(), 10);
            });
            $('#tp').html(sum);
        }
    </script>