Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form button fun 1

Status
Not open for further replies.

RedRobotHero

Technical User
May 2, 2003
94
0
0
CN
I have a form with two buttons. One is a submit button, and the other runs a function to calculate the total price of what was entered in the form.

I want to run the calculate function when I press enter. Right now, it submits the form. How do I change the actions performed when I hit enter?
 
Hi,
Give the javascript function you wish to call in the form action and ur problem is solved. Like this..

<form name=&quot;test1&quot; method=&quot;POST&quot; action=&quot;javascript:FocusMe()&quot; >
 
RedRobotHero,

I think this is working like you want with the enter doing
the calc and the image doing the submit, give it a try.

I took out the action for testing but you can repair that.

Code:
<HTML><HEAD><TITLE>Shopping Cart</TITLE>
<META http-equiv=Content-Type content=&quot;text/html; charset=windows-1252&quot;>
<SCRIPT language=JavaScript>
function calculate(form) {
form.pricehill.value = eval(form.numhill.value)*3.50
form.pricetract.value = eval(form.numtract.value)*0.50
}
function chkey(){
if(event.keyCode == 13){
  calculate(document.forms[0]);}
}
-->
</SCRIPT>
<STYLE type=text/css>.req {
	BACKGROUND-COLOR: #ffffff
}
.nreq {
	BACKGROUND-COLOR: #dddde9
}
</STYLE>
<META content=&quot;MSHTML 6.00.2800.1106&quot; name=GENERATOR></HEAD>
<BODY onkeypress=&quot;chkey();&quot;>
<FORM class=blue id=purchase method=get><INPUT 
class=req maxLength=5 value=0 name=numhill> $<INPUT class=nreq readOnly 
maxLength=5 value=0 name=pricehill> <INPUT class=req maxLength=5 value=0 
name=numtract> $<INPUT class=nreq readOnly maxLength=5 value=0 name=pricetract> 
<a href=&quot;#&quot; onclick=&quot;document.forms[0].submit();&quot;>
<img src=&quot;butncalc_files/x-click-but23.gif&quot; border=&quot;0&quot;></a>
<INPUT class=nreq onclick=calculate(this.form) type=button value=&quot;re-calculate price&quot;> 
</FORM></BODY></HTML>[\code]

2b||!2b
 
That gets me most of the way. It still submits when I press enter, but it does the calculation first, which is an improvement.

Is there any way for me to make it only submit when the user clicks the &quot;submit&quot; button? (which is now a link, rather than a true button)
 
<script>
function SubClicked()
{
return eval(document.Form1.h1.value)
}
function Change()
{
document.Form1.h1.value=&quot;true&quot;
SubClicked()
}
</script>
<form name=&quot;Form1&quot;.... onsubmit=&quot;return SubClicked()&quot;>
<input type=&quot;hidden&quot; name=&quot;h1&quot; value=&quot;false&quot;>
<input type=&quot;submit&quot; name=&quot;s1&quot; onclick=&quot;Change()&quot;>


try it...

Known is handfull, Unknown is worldfull
 
Hello, in my testing of the above page that wasn't a problem
with IE6.0.

Maybe you could upload what you wound up with at one of your
test sites.

Did you remove the &quot;type=image&quot;?

2b||!2b
 
It's right here:


And, I guess I was only having that problem in Opera. I just tested it in IE and it works fine. In Netscape, it has the opposite problem, that hitting enter does nothing.

Maybe I'll live with that as is, but if anyone knows a way to fix the browser inconsistancies...
 
Here's some changes you can try, please let me know if this
works in the other browsers...

Code:
function chkey(evt){
  if(evt.keyCode) {
  kcode = evt.keyCode;}
  else { if(evt.which) {
  kcode = evt.which; } }
    if(kcode == 13){
  calculate(document.forms[0]);}
}

and

<BODY onkeypress=&quot;chkey(event);&quot;>

2b||!2b
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top