473,385 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Simple for you : I cannot display my result

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
5 1033
jd
Try adding an onkeypress handler to quantity field.

-- jeff

Dec 10 '05 #2
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Wayne Davis | last post by:
I want to password protect the continued running of a section of javascript. What I need is for the user to see a login field, they type a code in, if it is good, the script continues, if bad, it...
1
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
2
by: slitchfield | last post by:
Sorry if this is real beginner's stuff, but I'm an old-school HTML guy and this is all another world to me. Is there an easy way to get the user to specify a number from 1 to 5 (in a pick list...
5
by: Mike | last post by:
Hello All, Please, if anyone can point me to the problem, I'd sure appreciate it! I am very new to VB programming and not a programmer to begin with. This is part of a Visual Basic 2005 Express...
6
mmarif4u
by: mmarif4u | last post by:
Hi everyone. i make a page that a user input thier icnumber with confirm ic number, it saves the data to mysql db with current date and a random access code generated automatically, NOW i have...
20
by: cmbcorp | last post by:
Hi, I have been playing around with a simple php login script and im getting an error message when i attempt to log in with the username and password i set in the sql table. The error message is...
3
by: Hazza | last post by:
Hi, I am using PHP and mysql to create a website. I am fairly new to PHP, and thus am grateful to anyone who helps! Firstly I am running a homepage, that displays additional content if a user...
4
by: Arun | last post by:
I came across a message sometime back in 2002. Here's a link:...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.