用js在html计算金额总和,JS计算多个input总和

满意答案

dcebd7a0de6265b6ccae5ead692f1eab.png

vcxdfw6ter

推荐于 2016.05.06

dcebd7a0de6265b6ccae5ead692f1eab.png

采纳率:46%    等级:7

已帮助:360人

||自己5261引入jquery

html>

extend = function (a, b) { //合并对象

if (typeof a !== "object" ||4102 typeof b !== "object") {

return;

}

for (key in b) {

a[key] = b[key];

}

return a;

};

extend(Math, {

/*************格式化数值***************/

format: function (num, style) {

if (!/#+([^#]+)?(#+)(?:\.(#+))?/.test(style)) {

return;

}

//分割符1653

var split = RegExp.$1;

//保留小数位数

num = parseFloat(num).toFixed(RegExp.$3.length);

var re = new RegExp('(\\d{1,' +  RegExp.$2.length + '})(?=(\\d{' +  RegExp.$2.length + '})+(?:$|\\.))', 'g');

//console.log(re);

if(!/(-?\d+)(?:\.(\d+))?/.test(num)){

return;

}

var d = (RegExp.$2 ? '.' + RegExp.$2 : ''),

str = RegExp.$1.replace(re, function (a, b, c, d) {

return b + split;

});

return str + d;

}

});

$(function(){

var total = 0;

$('.int').each(function(){

$(this).keypress(function(event){

if(!((event.charCode >= 48 && event.charCode <= 57) || event.charCode === 46 || event.charCode === 45 || event.charCode === 0)){

if(event.preventDefault){

event.preventDefault()

}else{

event.returnValue = false;

}

}

}).keyup(function(){

total = 0;

$('.int').each(function(){

if($(this).val() === '.' || $(this).val() === '-'){

return;

}

total += parseFloat($(this).val() || 0);

});

$('.total').html(Math.format(total,'#,###.##'));

});

});

});

总和:

追问: 如果序号不规则,也就是说html和JS分离的写法,只需要我所规定范围里的input数值进行求和

追答: 你规定的范围,自己写吧,我们只是写个引水作用

再说你提问也没有这样说,对于动态变更的需求,不予接纳,采纳即可

00分享举报

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值