开发者

How to calculate the total value of input boxes using jquery

开发者 https://www.devze.com 2023-01-15 17:34 出处:网络
I have a list of input boxes, now 开发者_C百科I need to calculate the total of all values entered in input boxes with the following naming convention pre[0],pre[1],pre[2] etc.

I have a list of input boxes, now 开发者_C百科I need to calculate the total of all values entered in input boxes with the following naming convention pre[0],pre[1],pre[2] etc.

Is this possible with Jquery and how?


Would something like this work?

var sum = 0;

$('input[name^="pre"]').each(function(){
    sum += parseFloat(this.value);
});

^= is the Attribute Starts With Selector.


I would do it like this

var sum = 0;

find("input[name*='pre']").each(function(index) {

sum = sum + this.val();

 })
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号