0

我正在使用带有母版页的 asp.net。我有一个产生 8 行的列表视图。我想使用jQuery做的是,当有人在单元格1-7中输入一个值时,当他们离开单元格时我想计算单元格1-7并将该值放入单元格8中。所以每一行都会有计算完成。我找到了一些循环遍历表格的代码

enter code here

$(document).ready(function () {
$('#ctl00_ContentPlaceHolder1_lvOccLine_ctrl0_tbltblOccLineList tr').each(function () {
    $(this).find('td').each(function () {
    })
})

});

但过去没有取得任何进展。在萤火虫中,我看到我试图获得的值在 this/cells/1/childnodes 中。看起来像这样

NodeList[input#ctl00_ContentPlaceHolder1_lvOccLine_ctrl0_txtCacasian attribute value = "1"]

呈现的 html 看起来像这样

<input type="text" style="width:100%;" id="ctl00_ContentPlaceHolder1_lvOccLine_ctrl0_txtCacasian" value="1" name="ctl00$ContentPlaceHolder1$lvOccLine$ctrl0$txtCacasian">

任何帮助都会很棒

4

3 回答 3

0

I am honestly not too familiar with ASP.NET, but in general jQuery terms, you could easily accomplish this by simply fetching cells by CLASSES, and update the 8th cell like that?

For example, 1) put a common class for your first 7 cells, ie "cell-to-fetch" 2) put a unique class for the last 8th cell, ie "cell-8" 3) on blur event of cell 7 (blur = focusOut http://api.jquery.com/blur/) just do a simple fetching and adding:

$(this).find('.cell-to-fetch').each(function () {
    $total += $(this).val();
})

4) and finally just update your cell-8 with the result you get, like this:

$('.cell-8').val($total);

Hope it helps, at least as a general concept.

于 2013-10-03T20:04:34.207 回答
0

演示

$(document).ready(function () {
    $('#ctl00_ContentPlaceHolder1_lvOccLine_ctrl0_tbltblOccLineList input').on('change', function () { /* bind change to input */
        var sum = 0,
            $this = $(this).parents('tr');
        $this.find('input').each(function() { /* find all inputs in the row */
            var value = parseInt(this.value);
            sum += value % 1 == 0 ? value : 0; /* add values together */
        });
        $this.find('td').last().text(sum); /* output sum into last column */
        return true;
    });
});
于 2013-10-03T20:07:11.597 回答
0

http://jsfiddle.net/dKxW8/

$(document).ready(function () {
    $("#calc").click(function () {
        //first get number of rows in the table (because we have one input per row)
        var count = $("#mytable tr").length;

        //loop through the rows and get the sum of every input value except the last
        var sum = 0;
        for (var i = 0; i < (count - 1); i++) {
            //get the value and add it to sum
            //check if its a number
            if(!isNaN(parseInt($("#mytable tr").eq(i).find("input").val(), 10))){
                sum += parseInt($("#mytable tr").eq(i).find("input").val(), 10);
            }
        }

        //assign the last input's value (in last row) to the sum
        $("#mytable tr").eq(count - 1).find("input").val(sum);
    });
});
于 2013-10-03T20:13:50.063 回答