[英]How do I get the sum of three inputs values in a span using JavaScript or jQuery?
[英]How do I use javascript or jquery to find a sum and product of values from a forms field inputs?
如何使用javascript或jquery查找用戶在表單字段中輸入的數字值的總和。 感謝您的時間和幫助。
輸入1值+輸入2值=輸入A
輸入A值* .08 =輸入B值
輸入A值+輸入B值=總輸入
<form>
<input type="text" id="1" name="1">
<input type="hidden" id="2" name="2" value="33">
<input type="text" id="A" name="A">
<input type="text" id="B" name="B">
<input type="text" id="total" name="total">
<button type="button" id="button" name="button">
</form>
IVE嘗試了什么
<script>
var $form = $('#contactForm'),
$summands = $form.find('.sum1'),
$sumDisplay = $('#itmttl');
$form.delegate('.sum1', 'change', function ()
{
var sum = 0;
$summands.each(function ()
{
var value = Number($(this).val());
if (!isNaN(value)) sum += value;
});
$sumDisplay.val(sum);
});
</script>
<script>
function multiply(one, two) {
if(one && two){
this.form.elements.tax.value = one * two;
} else {
this.style.color='blue';
}
}
</script>
請找到小提琴鏈接
$(document).ready(function(){
$('#calculate').on('click',function(){
var v1 = $('#1').val(); // take first text box value
var v2 = $('#2').val(); // take second hidden text box value
$('#A').val(parseInt(v1)+parseInt(v2)); // set value of A
var aval = (parseInt($('#A').val()) * parseFloat(.8)); // calculate value of b
$('#B').val(aval);// set value of B
var totalval = parseInt($('#A').val()) + parseFloat(aval);
//calculate value for total
$("#total").val(totalval); // set total
})
});
我假設你想更新的領域,當你舔按鈕? 創建了一個片段而不是小提琴:
$("#button").click(function() { $("#A").val( parseInt($("#1").val()) + parseInt($("#2").val()) ); $("#B").val(parseInt($("#A").val()) * .8); $("#total").val( parseInt($("#A").val()) + parseInt($("#B").val()) ); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="text" id="1" name="1"> <input type="hidden" id="2" name="2" value="33"> <input type="text" id="A" name="A"> <input type="text" id="B" name="B"> <input type="text" id="total" name="total"> <button type="button" id="button" name="button"> </form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.