admin管理员组文章数量:1244315
I'm trying to make a real-time calculator based on form inputs, when I use a sample div it works but I seem to be missing something when I try to print the total inside an input...
Working Markup Solution:
<input id='first' type="text" class="form-control formBlock" name="bus_ticket" placeholder="Bus Ticket..." required/><br />
<input id='second' type="text" class="form-control formBlock" name="plane_ticket" placeholder="Plane Ticket..." required/><br />
<input id='third' type="text" class="form-control formBlock" name="hotel_expenses" placeholder="Hotel Expenses..." required/><br />
<input id='fourth' type="text" class="form-control formBlock" name="eating_expenses" placeholder="Eating Expenses..." required/><br />
Total : <span id="total_expenses"></span>
Working Script
$('input').keyup(function(){ // run anytime the value changes
var firstValue = parseFloat($('#first').val()); // get value of field
var secondValue = parseFloat($('#second').val()); // convert it to a float
var thirdValue = parseFloat($('#third').val());
var fourthValue = parseFloat($('#fourth').val());
$('#total_expenses').html(firstValue + secondValue + thirdValue + fourthValue); // add them and output it
});
Non-Working Markup
<input id='total_expenses' type="text" class="form-control formBlock" name="funding" placeholder="Total Expenses..."/>
Non-Working Script
$('input').keyup(function(){ // run anytime the value changes
var firstValue = parseFloat($('#first').val()); // get value of field
var secondValue = parseFloat($('#second').val()); // convert it to a float
var thirdValue = parseFloat($('#third').val());
var fourthValue = parseFloat($('#fourth').val());
document.getElementById('#total_expenses').value(firstValue + secondValue + thirdValue + fourthValue);
// add them and output it
});
I'm trying to make a real-time calculator based on form inputs, when I use a sample div it works but I seem to be missing something when I try to print the total inside an input...
Working Markup Solution:
<input id='first' type="text" class="form-control formBlock" name="bus_ticket" placeholder="Bus Ticket..." required/><br />
<input id='second' type="text" class="form-control formBlock" name="plane_ticket" placeholder="Plane Ticket..." required/><br />
<input id='third' type="text" class="form-control formBlock" name="hotel_expenses" placeholder="Hotel Expenses..." required/><br />
<input id='fourth' type="text" class="form-control formBlock" name="eating_expenses" placeholder="Eating Expenses..." required/><br />
Total : <span id="total_expenses"></span>
Working Script
$('input').keyup(function(){ // run anytime the value changes
var firstValue = parseFloat($('#first').val()); // get value of field
var secondValue = parseFloat($('#second').val()); // convert it to a float
var thirdValue = parseFloat($('#third').val());
var fourthValue = parseFloat($('#fourth').val());
$('#total_expenses').html(firstValue + secondValue + thirdValue + fourthValue); // add them and output it
});
Non-Working Markup
<input id='total_expenses' type="text" class="form-control formBlock" name="funding" placeholder="Total Expenses..."/>
Non-Working Script
$('input').keyup(function(){ // run anytime the value changes
var firstValue = parseFloat($('#first').val()); // get value of field
var secondValue = parseFloat($('#second').val()); // convert it to a float
var thirdValue = parseFloat($('#third').val());
var fourthValue = parseFloat($('#fourth').val());
document.getElementById('#total_expenses').value(firstValue + secondValue + thirdValue + fourthValue);
// add them and output it
});
Share
Improve this question
asked Mar 11, 2017 at 2:32
Sebastian FarhamSebastian Farham
8252 gold badges13 silver badges27 bronze badges
0
3 Answers
Reset to default 9Here's a working JSFiddle.
You've been confusing Javascript and jQuery codes.
// Wrong code:
document.getElementById('#total_expenses').value(sum)
//Correct code:
document.getElementById('total_expenses').value() = sum
Also, change parseFloat
to Number
so you don't get NaN
when at least one of your input fields is blank.
BONUS: change your <input type="text" />
to <input type="number" />
to prevent "non-numerical" inputs.
You can try this jquery code
var totalValue = firstValue + secondValue + thirdValue + fourthValue;
$('#total_expenses').val(totalValue);
First, when using getElementById you can omit the #. Secondly, assigning a value to an input using vanilla JS is done by simple assignment, and not a function call. So that line should look like this:
document.getElementById('total_expenses').value = firstValue + secondValue + thirdValue + fourthValue;
本文标签: jqueryReal Time Javascript Calculation Based On Form InputsStack Overflow
版权声明:本文标题:jquery - Real Time Javascript Calculation Based On Form Inputs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740203121a2240579.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论