1

I am having a grid view as follows

 Quantity    Description    Rate    Total
   1             ---         10      10
   2             ---         20      40

                             Label outside the `gridview`

I have written a javascript to display total now I would like to display the total like 10+40 and should display as 50 like that I will have some other ways what I need is I would like to display the total sum on Blur event of quantity textbox.

enter image description here

My code for displaying amount onblur event is as follows

<script type="text/javascript">
    function multiplication(txtQuantity, txtRate, txtAmount) {
        var weight = document.getElementById(txtQuantity).value;
        var rate = document.getElementById(txtRate).value;
        document.getElementById(txtAmount).value = weight * rate;
    }
</script>

In this script I would like to include the total

4

1 回答 1

1

给数量文本框起一个名字,比如“数量”,然后你可以得到它们:

document.getElementsByName("quantity");

给你的标签一个 id,比如“total”,这样你就可以得到它:

document.getElementById("total")

使用正文 onload 事件将事件侦听器附加到数量文本框的 onblur 事件。当您使用它时,请使用命名空间、单例,这样您就不会弄乱全局命名空间,如下所示:

<script>
    var p = {
        onload: function() {
            var els_qty = document.getElementsByName("quantity");
            for(var i = 0, ceiling = qty_els.length; i < ceiling; i++) {
                qty_els[i].onblur = function() {
                    var total = 0;
                    for(var j = 0; j < ceiling; j++) {
                        total += Number(qty_els[j].value);
                    }
                    document.geTElementById("total").innerHTML = total;
                }
            }
        }
    };
</script>

要附加 onload 事件侦听器,请使用以下命令:

<body onload="p.onload()">

</body>

您可能想对数量文本框进行一些验证,但我将其留给您。

于 2012-04-11T05:40:49.407 回答