简体   繁体   中英

Javascript calculate the sum of all input fields with a specific class

Okay, i have this code which is working fine to a specific point.

    $(document).ready(function(){
          var val1 = +$(".charge").val();
          var val2 = +$(".fee").val();
          var val3 = +$(".loadrate").val();
          $("#finalrate").val(val1-val2+val3);
    $(".financeinput").keyup.each(function(){
          var val1 = +$(".charge").val();
          var val2 = +$(".fee").val();
          var val3 = +$(".loadrate").val();
          var initial = $("#finalrate").val();

          $("#finalrate").val(val1-val2+val3);

          var second = $("#finalrate").val();

          if(initial < second){

          $("#finalrate").removeClass("redardown").addClass("greenarup");

          }
          if(initial > second){

          $("#finalrate").removeClass("greenarup").addClass("redardown");

          }
          if(initial = second){

          }

    });
});

I have multiple fields with class ".charge" and multiple fields with class ".fee" but the code here is getting only the values of the first fields with that class, while i need all of them to be calculated. I know this must be very easy, but i am fairly new to javascript.

You should use .each on your 'charge' and 'fee' inputs.

$(".charge").each(function()
{
    $(this).val(); // this should have value for current input
}

here is a link that shows how to use the each function

Here is an example so you can see how it works:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<title>Login</title>
</head>
<body>
<input class="test" type="text" value="2">
<input class="test" type="text" value="3">
<input class="test" type="text" value="4">
</body>
<script type="text/javascript">
var test1 = 0;
$(".test").each(function(){
    test1 += parseInt($(this).val());
});
window.alert("" + test1);
</script>
</html>

I'm not sure exactly what you are trying to do but you might want something like this:

var val1 = 0;
$(".charge").each(function(){
     val1 += parseInt($(this).val());
});
window.alert("" + val1); // should have all .charge added up here

this is just a pseudocode but basically your solution may look like this (pure js)

var x = document.getElementsByClassName("charge");
for(var i = 0; i<x.length; i++){
   sum += x[i].value;
}

and just do that for all the classes you need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM