简体   繁体   English

使用javascript输入字段中所有值的总和

[英]Sum of all values in the input field using javascript

The value you entered in the input field id="thousands" will be multiplied by 1000 and the result will be displayed to the input field id="thousands-output" . 您在输入字段id="thousands"输入的值将乘以1000,结果将显示在输入字段id="thousands-output" That part of the code is working perfectly. 该部分代码运行正常。 What i want is to get the sum of the value of all the multiplied outputs and display it in the input field id="totalBills" . 我想要的是获取所有乘积输出的值的总和,并将其显示在输入字段id="totalBills" I cant seem to solve this issue. 我似乎无法解决这个问题。 Any help would be appreciated 任何帮助,将不胜感激

HTML Code: HTML代码:

     <label>1000</label>
         <div class="input-group">
            <input type="number" class="form-control" name="thousands" id="thousands" />
            <span class="input-group-addon">=</span>
            <input type="text" class="form-control txt" id="thousands-output" style="width:70%;" readonly />
         </div>

     <label>500</label>
         <div class="input-group">
            <input type="number" class="form-control" name="five_hundreds" id="five_hundreds"/>
            <span class="input-group-addon">=</span>
            <input type="text" class="form-control txt" id="five_hundreds-output" style="width:70%;" readonly />
         </div>  

<input type="total" id="totalBills" class="form-control" value="0">

Javascript Code: JavaScript代码:

   var $output = $("#thousands-output");
    $("#thousands").keyup(function() {
        var value = parseFloat($(this).val());    
        $output.val(value*1000);
    });


      var $output2 = $("#five_hundreds-output");
    $("#five_hundreds").keyup(function() {
        var value = parseFloat($(this).val());       
        $output2.val(value*500);
    });

Use querySelectorAll and reduce 使用querySelectorAllreduce

//all the outputs are the child of input-group and has type="text"
var allOutputs = document.querySelectorAll( ".input-group input[type='text'].txt" );

//iterate all the output values. If the value is a number, add to the accumulator, else add 0
var sum = [ ...allOutputs ].reduce( ( a, c ) => a + ( isNaN( c.value ) ? 0 : +c.value ) , 0);

//now display this value
document.getElementById( "totalBills" ).value = sum;

You need to fetch both the values and add it and set it as the value of the input field. 您需要同时获取这两个值并将其添加并将其设置为输入字段的值。

function getSum(){
  var sum = +$("#thousands-output").val() + +$("#five_hundreds-output").val();
  $("#totalBills").val(sum);
}

Call this function inside both the keyup events 在两个keyup事件中调用此函数

 var $output = $("#thousands-output"); $("#thousands").keyup(function() { var value = parseFloat($(this).val()); $output.val(value * 1000); getSum(); }); var $output2 = $("#five_hundreds-output"); $("#five_hundreds").keyup(function() { var value = parseFloat($(this).val()); $output2.val(value * 500); getSum(); }); function getSum(){ var sum = +$("#thousands-output").val() + +$("#five_hundreds-output").val(); $("#totalBills").val(sum); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label>1000</label> <div class="input-group"> <input type="number" class="form-control" name="thousands" id="thousands" /> <span class="input-group-addon">=</span> <input type="text" class="form-control txt" id="thousands-output" style="width:70%;" readonly /> </div> <label>500</label> <div class="input-group"> <input type="number" class="form-control" name="five_hundreds" id="five_hundreds" /> <span class="input-group-addon">=</span> <input type="text" class="form-control txt" id="five_hundreds-output" style="width:70%;" readonly /> </div> <input type="total" id="totalBills" class="form-control" value="0"> 

Update: 更新:

Using unary operator + to convert string to numbers. 使用unary+将字符串转换为数字。

 $(document).ready(function(){ var $output = $("#thousands-output"); $("#thousands").keyup(function() { var value = getValue($(this)); $output.val(value*1000); refreshTotal(); }); var $output2 = $("#five_hundreds-output"); $("#five_hundreds").keyup(function() { var value = getValue($(this)); $output2.val(value*500); refreshTotal(); }); }); function getValue(elem){ var value = parseFloat($(elem).val()); if (isNaN(value)){ value = 0; } return value; } function refreshTotal(){ var fi = getValue($("#five_hundreds-output")); var th = getValue($("#thousands-output")); $("#totalBills").val(fi+th); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label>1000</label> <div class="input-group"> <input type="number" class="form-control" name="thousands" id="thousands" /> <span class="input-group-addon">=</span> <input type="text" class="form-control txt" id="thousands-output" style="width:70%;" readonly /> </div> <label>500</label> <div class="input-group"> <input type="number" class="form-control" name="five_hundreds" id="five_hundreds"/> <span class="input-group-addon">=</span> <input type="text" class="form-control txt" id="five_hundreds-output" style="width:70%;" readonly /> </div> <input type="total" id="totalBills" class="form-control" value="0"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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