admin管理员组文章数量:1291123
I'm looking to create a pretty simple calculator, but I need it to update on each keystroke. I cannot seem to find anything in that specific category. Can anyone point me to the right direction?
I'm looking for something like A*1.325 + B*3.76 where B is a drop down menu and A is the text field that people will be filling out. Every time the drop down is changed or a keystroke is registered in the text box.
I will also try to do some RegEx to only allow numbers or decimal points!
Thank you guys very much!!!
PS. Will be using PHP/HTML to create the form.
I'm looking to create a pretty simple calculator, but I need it to update on each keystroke. I cannot seem to find anything in that specific category. Can anyone point me to the right direction?
I'm looking for something like A*1.325 + B*3.76 where B is a drop down menu and A is the text field that people will be filling out. Every time the drop down is changed or a keystroke is registered in the text box.
I will also try to do some RegEx to only allow numbers or decimal points!
Thank you guys very much!!!
PS. Will be using PHP/HTML to create the form.
Share Improve this question asked Jul 27, 2012 at 22:43 JEFF BJEFF B 1262 silver badges12 bronze badges 4- 1 You should ask a much more specific question. What have you done so far? Where are you stuck? – Explosion Pills Commented Jul 27, 2012 at 22:46
- 2 Nope, that's not AJAX. AJAX is when you need to asynchronously get data from the server (think Google Instant). – Linuxios Commented Jul 27, 2012 at 22:46
- Is the equation given or input by user? – Bergi Commented Jul 27, 2012 at 22:49
-
Try
onkeypress
andonchange
(HTML properties) to trigger a function. – A.M.K Commented Jul 27, 2012 at 22:52
5 Answers
Reset to default 3Step 1:
Do the jQuery tutorials. fun fun fun :D :D :D http://docs.jquery./Tutorials
Step 2:
Now that you understand jQuery, notice that jQuery supports a bunch of event handlers. For instance, you can assign a click event to something that resembles an item in a dropdown menu:
$("itemInMyDropDownMenu").click(function(e) {
doSomeCalculation(parseFloat(this.val()));
}
Step 3:
Chose the right event handlers to use. click
(for the dropdown menu) and keyup
(for the text field) sound hopeful.
Step 4:
Keep tinkering. You don't need PHP at all.
The first question. If you need just simple calculator, why would you need AJAX? AJAX can send requests to web pages and scripts and get the XML, JSON, html or simple text responses.
If you still need AJAX, read http://www.w3schools./ajax/default.asp
Realtime updates: Set the id of elements you are going to work with. Create the appropriate handler, which is called any time the field is changed.
<input type="text" id="field" value="" onChange="javascript: setA(this)"/>
function setA(obj){
var a = 0 ;
if (obj.value)
a = obj.value ;
//call any function to calculate anything and send a.
}
If you want to display something, here id es to be handy.
<div id="result"></div>
document.getElementById("result").innerHTML = 345 ; //just set to what you need
You can fully utilize javascript for building simple calculator without submitting the form, as it can work in real time.
Hope it helps :)
While I waiting to hear back I did a little more research and found some code that I think will work for what I was looking for. I was overthinking the whole thing!!
<html>
<head>
</head>
<body>
<script>
function doMath() {
var totalparts = parseInt(document.getElementById('parts_input').value);
var labor = parseInt(document.getElementById('labor_input').value);
var misc = parseInt(document.getElementById('misc_input').value);
var subtotal = totalparts + labor + misc;
var tax = subtotal * .13;
var total = subtotal + tax;
document.getElementById('subtotal_input').value = subtotal;
document.getElementById('tax_input').value = tax;
document.getElementById('total_input').value = total;
}
</script>
<div>Total Parts: <input type="text" id="parts_input" value="1" readonly="true" /></div>
<div>Labor: <input type="text" id="labor_input" onBlur="doMath();" /></div>
<div>Misc: <input type="text" id="misc_input" onBlur="doMath();" /></div>
<div>Sub Total: <input type="text" id="subtotal_input" readonly="true" /></div>
<div>Tax: <input type="text" id="tax_input" readonly="true" /></div>
<div>Total: <input type="text" id="total_input" readonly="true" /></div>
</body>
</html>
You don't need to use, and shouldn't use, AJAX for this. Instead, you should do something like this:
$("#textfieldID, #dropDownMenuID").change(function() {
var aVal = $("#textFieldID").val() == "" ? 0 : parseFloat($("#textFieldID").val());
var bVal = $("#dropDownMenuID").val() == "" ? 0 : parseFloat($("#dropDownMenuID").val());
$("#answerID").text((aVal*1.325 + bVal*3.76).toString());
});
Have you tried onchange="calculator()"
?
本文标签: phpjQueryJavascript calculator that autoupdates realtime (guessing AJAX)Stack Overflow
版权声明:本文标题:php - jQueryJavascript calculator that auto-updates realtime (guessing AJAX) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741516148a2382900.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论