jQuery的多个运行总计

编程入门 行业动态 更新时间:2024-10-28 02:30:43
本文介绍了jQuery的多个运行总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

0I我使用jQuery来计算多个文本框的运行总和。刚刚找到如何获取工作,前几天一个真棒响应,但现在我遇到了另一个问题。当使用一个选择,对于GetTotal总量是完全计算。然而,当我有第二个选择,总计开始的冲突与彼此,不再计算正确。我一直在寻找一个解决的办法有一段时间了,没有任何人有什么想法?

下面是我目前使用的选择:

函数GetTotal(txtBox){        无功总= 0;        $(输入:文本),每个(函数(指数值){。            总+ = parseInt函数($(值).VAL()|| 0);        });        $(#chkTotal)的HTML(总);    }

我的观点使用这些TXT盒

< D​​IV CLASS =主编场>        @ Html.TextBox(字段1的String.Empty,新{inputType下=TEXT,ID =字段1的onchange =GetTotal(本)})    < / DIV>    < D​​IV CLASS =主编场>        @ Html.TextBox(字段2的String.Empty,新{inputType下=TEXT,ID =场2的onchange =GetTotal(本)})    < / DIV>    < D​​IV>        < H3个总选中< / H3 GT&;    < / DIV>< D​​IV ID =chkTotal>< / DIV>

现在我想实现的另一个选择,这将总共两个额外的编辑栏...

函数GetTotal1(txtBox){        VAR共1 = 0;        $(输入:文本),每个(函数(指数值){。            共1 + = parseInt函数($(值).VAL()|| 0);        });        $(#disTotal)HTML(共1页)。    }

查看:

< D​​IV CLASS =主编场>        @ Html.TextBox(字段3,的String.Empty,新{inputType下=TEXT,ID =FIELD3的onchange =GetTotal1(本)})    < / DIV>    < D​​IV CLASS =主编场>        @ Html.TextBox(字段4的String.Empty,新{inputType下=TEXT,ID =字段4的onchange =GetTotal1(本)})    < / DIV>    < D​​IV>        < H3个总分布式< / H3 GT&;    < / DIV>    < D​​IV ID =disTotal>< / DIV>

解决方案

在两个和使用不同的HTML类,如

< D​​IV CLASS =主编场>    @ Html.TextBox(字段1的String.Empty,新{@class =共有的inputType =TEXT,ID =字段1的onchange =GetTotal(本)})< / DIV>< D​​IV CLASS =主编场>    @ Html.TextBox(字段2的String.Empty,新{@class =共有的inputType =TEXT,ID =场2的onchange =GetTotal(本)})< / DIV>< D​​IV>    < H3个总选中< / H3 GT&;< / DIV>< D​​IV ID =chkTotal>< / DIV>< D​​IV CLASS =主编场>    @ Html.TextBox(字段3,的String.Empty,新{@class =共1的inputType =TEXT,ID =FIELD3的onchange =GetTotal1(本)})< / DIV>< D​​IV CLASS =主编场>    @ Html.TextBox(字段4的String.Empty,新{@class =共1的inputType =TEXT,ID =字段4的onchange =GetTotal1(本)})< / DIV>< D​​IV>    < H3个总分布式< / H3 GT&;< / DIV>< D​​IV ID =disTotal>< / DIV>

使用Javascript:

函数GetTotal(txtBox){    无功总= 0;    $('输入:text.total0')。每个(函数(指数值){        总+ = parseInt函数($(值).VAL()|| 0);    });    $(#chkTotal)的HTML(总);}功能GetTotal1(txtBox){    VAR共1 = 0;    $('输入:text.total1')。每个(函数(指数值){        共1 + = parseInt函数($(值).VAL()|| 0);    });    $(#disTotal)HTML(共1页)。}

0I am using jQuery to calculate a running total on multiple textboxes. Just found an awesome response on how to get that working a few days ago, but now I am running into another problem. When using one selector, the total for GetTotal is calculated perfectly. However, when I include the second selector, the totals begin to conflict with one another, and no longer calculate properly. I have been searching for a solution to this for some time now, does anyone have any ideas?

Here is the selector i am currently using:

function GetTotal(txtBox) { var total = 0; $('input:text').each(function(index, value) { total += parseInt($(value).val() || 0); }); $("#chkTotal").html(total); }

My view uses these txt boxes

<div class="editor-field"> @Html.TextBox("Field1", String.Empty, new {InputType = "text", id = "field1", onchange = "GetTotal(this)" }) </div> <div class="editor-field"> @Html.TextBox("Field2", String.Empty, new {InputType = "text", id = "field2", onchange = "GetTotal(this)" }) </div> <div> <h3>Total Checked</h3> </div> <div id="chkTotal"></div>

Now I am trying to implement another selector which will total two additional editor fields...

function GetTotal1(txtBox) { var total1 = 0; $('input:text').each(function (index, value) { total1 += parseInt($(value).val() || 0); }); $("#disTotal").html(total1); }

View:

<div class="editor-field"> @Html.TextBox("Field3", String.Empty, new {InputType = "text", id = "field3", onchange = "GetTotal1(this)" }) </div> <div class="editor-field"> @Html.TextBox("Field4", String.Empty, new {InputType = "text", id = "field4", onchange = "GetTotal1(this)" }) </div> <div> <h3>Total Distributed</h3> </div> <div id="disTotal"></div>

解决方案

Use different HTML classes on the two sums, like

<div class="editor-field"> @Html.TextBox("Field1", String.Empty, new {@class = "total0", InputType = "text", id = "field1", onchange = "GetTotal(this)" }) </div> <div class="editor-field"> @Html.TextBox("Field2", String.Empty, new {@class = "total0", InputType = "text", id = "field2", onchange = "GetTotal(this)" }) </div> <div> <h3>Total Checked</h3> </div> <div id="chkTotal"></div> <div class="editor-field"> @Html.TextBox("Field3", String.Empty, new {@class = "total1", InputType = "text", id = "field3", onchange = "GetTotal1(this)" }) </div> <div class="editor-field"> @Html.TextBox("Field4", String.Empty, new {@class = "total1", InputType = "text", id = "field4", onchange = "GetTotal1(this)" }) </div> <div> <h3>Total Distributed</h3> </div> <div id="disTotal"></div>

Javascript:

function GetTotal(txtBox) { var total = 0; $('input:text.total0').each(function(index, value) { total += parseInt($(value).val() || 0); }); $("#chkTotal").html(total); } function GetTotal1(txtBox) { var total1 = 0; $('input:text.total1').each(function (index, value) { total1 += parseInt($(value).val() || 0); }); $("#disTotal").html(total1); }

更多推荐

jQuery的多个运行总计

本文发布于:2023-11-22 04:19:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615954.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!