Topic: JS Function & Variable Storage (Page 1 of 1) |
|
---|---|
Neurotic (0) Inmate Newly admitted From: |
posted 05-14-2006 06:20
Hi, code: <html> <head> <title>Test</title> </head> <script language="javascript" type="text/javascript"> var current_charges=0; var running_total=0; var hours; function calculateCharges(hours) { if ( hours <= 3 ) { current_charges = 2; } else if ( hours > 3 && hours <= 19) { current_charges = 2 + (0.50 * (hours - 3)); } else { current_charges = 10; } document.parking.current.value=current_charges; running_total=running_total + current_charges; document.parking.running.value=running_total; [b]alert("current_charges:" + current_charges + " running_total: " + running_total);[/b] return false; } </script> <body> <form name="parking" method="get" onSubmit="calculateCharges(document.parking.hoursparked.value)"> Please enter the number of hours parked for the current customer: <br> <input type="Integer" name="hoursparked" value=""> <br> <input type="Submit" value="Calculate"> <p> Charges for current customer: <textarea name="current" rows="5" cols="5" value=""> </textarea> <p> Accumulated charges: <textarea name="running" rows="5" cols="5" value=""> </textarea> </form> </body> </html>
|
Paranoid (IV) Inmate From: Norway |
posted 05-14-2006 11:16
Normal, you don't return FALSE onSubmit to cancel the submission of the form, thus the values you see added in the GET parameters of the URL. Just change the FORM tag like that : code: <form name="parking" method="get" onSubmit="return calculateCharges(this.hoursparked.value)"> And voilà it works. |
Paranoid (IV) Inmate From: Norway |
posted 05-14-2006 11:44
Couldn't resist: code: <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <head> <title>Test</title> <script type="text/javascript"> var running_total=0; function calculateCharges( hoursArgument ) { var hours= parseFloat(hoursArgument); if( isNaN(hours) || hours<=0 ) { alert( "** ERROR **" ) return false; } var current_charges; if ( hours <= 3 ) current_charges = 2; else if ( hours > 3 && hours <= 19) current_charges = 2 + (0.50 * (hours - 3)); else current_charges = 10; running_total += current_charges; document.getElementById('currentWage').firstChild.nodeValue = current_charges; document.getElementById('runningWage').firstChild.nodeValue = running_total; return true; } </script> </head> <body> <form action=""> <fieldset> <legend>Please enter the number of hours parked for the current customer:</legend> <input type="text" name="hoursparked" value="0" /> <button type='button' onclick='calculateCharges(this.form.hoursparked.value)' >Calculate charges</button> </fieldset> </form> <p>Charges for current customer: <span id='currentWage'>0</span></p> <p>Accumulated charges: <span id='runningWage'>0</span></p> </body> </html>
|
Obsessive-Compulsive (I) Inmate From: |
posted 05-19-2006 21:45
Thanks for the help. I switched it up by using the button instead. I tried your example, but sometimes you would have to click twice for it to work. code: <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <head> <title>TME 2 Part 4</title> <script language="Javascript" type="text/javascript"> var current_charges=0; var running_total=0; var hours; var hoursparked; function calculateCharges(hours) { if ( hours <= 3 ) { current_charges = 2; } else if ( hours > 3 && hours <= 19) { current_charges = 2 + (0.50 * (hours - 3)); } else { current_charges = 10; } document.parking.current.value="$" + current_charges; running_total=running_total + current_charges; document.parking.running.value="$" + running_total; } </script> </head> <body> <form name="parking" onSubmit="return false;"> Please enter the number of hours parked for the current customer: <br> <input type="text" name="hoursparked" value=""> <br> <input type="button" onClick="calculateCharges(document.parking.hoursparked.value);" value="Calculate"> <p> Charges for current customer: <input type="text" name="current" value=""> <p> Yesterday's running total: <input type="text" name="running" value=""> </form> </body> </html> |
Paranoid (IV) Inmate From: Cold Sweden |
posted 05-19-2006 22:10
quote:
code: <form name="parking" onSubmit="calculateCharges(document.parking.hoursparked.value); return false;"> Please enter the number of hours parked for the current customer: <br> <input type="text" name="hoursparked" value=""> <br> <input type="submit" value="Calculate"> <p> Charges for current customer: <input type="text" name="current" value=""> <p> Yesterday's running total: <input type="text" name="running" value=""> </form>
|