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

时间:2018-02-23 09:40:27

标签: javascript jquery

您在输入字段id="thousands"中输入的值将乘以1000,结果将显示在输入字段id="thousands-output"中。代码的这一部分工作得很好。我想要的是获得所有乘法输出的值的总和,并将其显示在输入字段id="totalBills"中。我似乎无法解决这个问题。任何帮助将不胜感激

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代码:

   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);
    });

3 个答案:

答案 0 :(得分:1)

使用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;

答案 1 :(得分:1)

您需要获取这两个值并添加它并将其设置为输入字段的值。

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

keyup事件

中调用此函数

&#13;
&#13;
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);
}
&#13;
<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">
&#13;
&#13;
&#13;

<强>更新

使用unary运算符+将字符串转换为数字。

答案 2 :(得分:0)

&#13; &#13; $(文件)。就绪(函数(){&#13;     var $ output = $(&#34;#thousands-output&#34;);&#13;     $(&#34;#thousands&#34;)。keyup(function(){&#13;         var value = getValue($(this));&#13;         $ output.val(值* 1000);&#13;         refreshTotal();&#13;     });&#13;     var $ output2 = $(&#34;#five_hundreds-output&#34;);&#13;     $(&#34;#five_hundreds&#34;)。keyup(function(){&#13;         var value = getValue($(this)); &#13;         $ output2.val(值* 500);&#13;         refreshTotal();&#13;     }); &#13; });&#13; &#13; function getValue(elem){&#13;   var value = parseFloat($(elem).val());&#13;   if(isNaN(value)){&#13;     value = 0;&#13;   }&#13;   返回值;&#13; }&#13; &#13; function refreshTotal(){&#13;   var fi = getValue($(&#34;#five_hundreds-output&#34;));&#13;   var th = getValue($(&#34;#thousands-output&#34;));&#13;   $(&#34;#totalBills&#34)VAL(FI + TH);。&#13; }&#13; &lt; script src =&#34; https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt; / script&gt;&#13; &LT;标签&gt; 1000℃; /标签&gt;&#13;          &lt; div class =&#34; input-group&#34;&gt;&#13;             &lt; input type =&#34; number&#34;类=&#34;形状控制&#34;命名=&#34;千&#34; ID =&#34;千&#34; /&GT;&#13;             &lt; span class =&#34; input-group-addon&#34;&gt; =&lt; / span&gt;&#13;             &lt; input type =&#34; text&#34; class =&#34; form-control txt&#34; ID =&#34;千输出&#34;风格=&#34;宽度:70%;&#34; readonly /&gt;&#13;          &LT; / DIV&GT;&#13; &#13;      &LT;标签&gt; 500℃; /标签&gt;&#13;          &lt; div class =&#34; input-group&#34;&gt;&#13;             &lt; input type =&#34; number&#34;类=&#34;形状控制&#34;命名=&#34; five_hundreds&#34; ID =&#34; five_hundreds&#34; /&GT;&#13;             &lt; span class =&#34; input-group-addon&#34;&gt; =&lt; / span&gt;&#13;             &lt; input type =&#34; text&#34; class =&#34; form-control txt&#34; ID =&#34; five_hundreds输出&#34;风格=&#34;宽度:70%;&#34; readonly /&gt;&#13;          &LT; / DIV&GT; &#13; &#13; &lt; input type =&#34; total&#34; ID =&#34; totalBills&#34;类=&#34;形状控制&#34;值=&#34; 0&#34;&GT;&#13; &#13; &#13;