开发者

Real time updating of values on a form

开发者 https://www.devze.com 2023-03-14 20:55 出处:网络
Slightly related to my other question (which was answered) here, I was wondering if the following was possible (it likely is and 开发者_如何学Pythonvery basic but I\'m a bit noobish at JavaScript)!

Slightly related to my other question (which was answered) here, I was wondering if the following was possible (it likely is and 开发者_如何学Pythonvery basic but I'm a bit noobish at JavaScript)!

So I have 2 input fields where the user types something into a form. They press "calculate" and then the function totals up what they have entered and then calculates 1,2 and 3% for each value respectively. It looks something like this:

input 1%value

input 2%value

input 3%value

total total

Is there a way for me to be able to update the form in real time? So by this I mean, as soon as the user enters a value into a field the function automatically starts updating the values such as total etc? How would I go about achieving this, can it be done purely in JavaScript?

Thanks


If you want to display 'realtime' (meaningly, as the user types) values, you can use the keyup values :

  • http://www.w3schools.com/jsref/event_onkeyup.asp (for pure javascript)
  • http://api.jquery.com/keyup/ (for jquery)


Place an event handler on the onBlur event. If you had a Javascript function called calculateStuff(), your input element could reference it like this:

<input name="something" type="text" onblur="calculateStuff();" value="">

The onBlur event happens when the user leaves the field. If you want it to happen as they are still typing, you could use the onChange handler in the same way.


Yes. You should call an onkeyup / onchange event in JavaScript to determine if the user has typed anything and inside the event just have it call a JavaScript function which refreshes the form by doing the math and inserting the values.

You can also add other event listeners such as blur etc.

It has been a while so i cant post any usable code but Google is your friend here.


Without building a complete answer for you here are some hints:

pure javascript would require something like this using the .value from an element:

alert(document.getElementById('elementid').value);

if you use a javascript library as for example jquery you can use something as .val()

edit: you can use the onchange event to process the changes


Use onchange event handler. Simplex code are given below :

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
function summation()
{
    var input1 = document.getElementById('input1').value;
    var input2 = document.getElementById('input2').value;
    var input3 = document.getElementById('input3').value;
    var total = (input1*1) + (input2*1) +(input3*1);
    document.getElementById('total').innerHTML = "Total = $"+total;
}
</script> 
</head>
<body>
<p>Input 1 : <input id="input1" type="number"  onchange="summation()"></p>
<p>Input 2 : <input id="input2" type="number"  onchange="summation()"></p>
<p>Input 3 : <input id="input3" type="number"  onchange="summation()"></p>
<p id="total"></p>
</body>


It's simple! Instead of using the button, add an onChange="your_calculate_function()" attribute to each of your inputs.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号