Simple for you : I cannot display my result

R

RobG

I have an Order form. When I change the quantity I go to a script to
calculate anddisplay the Amount = UnitPrice * Quantity

But the Amount only display if I click somewhere else as the Quantity.

That is how onchange is supposed to work - it fires when the element
loses focus if its value has changed since gaining focus.

How can I display it without clicking ?

Use onkeyup, but the new value will be written to mt on every keyup, so
make sure you account for intermediate results that may not be valid and
don't allow errors to foul things up.

============================================
<SCRIPT LANGUAGE="JavaScript">

The language attribute is deprecated, type is required:

function compute(form1) {
form1.mt.value=parseFloat(form1.up.value)*parseInt(form1.qt.value);

Even though you set 'up' as readonly, a user can still change the value,
do not assume that it is a valid float or integer. A much better idea
is to test the value of 'up' to make sure it is OK before using it -
similarly with the value of 'qt'.

The values of up and qt do not need to be explicitly converted to
numbers since they are multiplied (addition is a different matter).

Your script could be:


function compute(form1)
{
var x = form1.up.value;
var y = form1.qt.value;
if (validFloat(x) && validInt(y))
form1.mt.value = x*y;
}
}

Where validFloat() and validInt() are routines that check the values -
you can find routines to do that in the archives or at:

<URL: http://www.merlyn.demon.co.uk >


I'd give you a better link to the number validation routines but the
site seems to be down at the moment.


[...]
 
R

RobG

jd said:
Try adding an onkeypress handler to quantity field.

No, don't. That will fire before the value has been changed, so the
last keypress will not be included in the result (unless it's a tab or
similar).
 
D

degnau

I have an Order form. When I change the quantity I go to a script to
calculate anddisplay the Amount = UnitPrice * Quantity

But the Amount only display if I click somewhere else as the Quantity.
How can I display it without clicking ?

============================================
<SCRIPT LANGUAGE="JavaScript">
function compute(form1) {
form1.mt.value=parseFloat(form1.up.value)*parseInt(form1.qt.value);
}
</SCRIPT>
<body>
<form name="form1" >
<input name="up" type="text" id="up" value="15" readonly="">
<input name="qt" type="text" id="qt" onchange="compute(this.form);">
<input name="mt" type="text" id="mt" onfocus="this.blur()">
</form>
============================================
 
D

Dr John Stockton

JRS: In article <439a6587$0$22277$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Sat, 10 Dec 2005 15:17:35 local, seen in
news:comp.lang.javascript said:
Where validFloat() and validInt() are routines that check the values -
you can find routines to do that in the archives or at:

<URL: http://www.merlyn.demon.co.uk >


I'd give you a better link to the number validation routines but the
site seems to be down at the moment.

The index page should do, but

<URL: http://www.merlyn.demon.co.uk/js-index.htm>
<URL: http://www.merlyn.demon.co.uk/js-valid.htm>

are more direct.

The site should be up AFAIK; but there is a daily bandwidth limitation
and you may be seeing its effects.

P.S. OTOH, reading another newsgroup, I see that the servers at Demon
have been failing this weekend.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top