By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,473 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,473 IT Pros & Developers. It's quick & easy.

Simple for you : I cannot display my result

P: n/a
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>
============================================

Dec 10 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
jd
Try adding an onkeypress handler to quantity field.

-- jeff

Dec 10 '05 #2

P: n/a
de****@loxinfo.co.th wrote:
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.

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

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:

<script type="text/javascript">

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.
[...]
--
Rob
Dec 10 '05 #3

P: n/a
jd wrote:
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).

--
Rob
Dec 10 '05 #4

P: n/a
It works, Thanks

On Sat, 10 Dec 2005 11:26:19 +0100, de****@loxinfo.co.th wrote:
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)*parseIn t(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>
============================================


Dec 10 '05 #5

P: n/a
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, RobG <rg***@iinet.net.au> posted :

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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
Dec 11 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.