STRUTS- <html:submit> does not submit on pressing Enter key

M

Megha Vishwanath

Hi,

I have a struts form <html:form> in which although the focus rests on
the submit button, hitting the enter key does not submit the form.

I tried capturing the Enter key event and followed it by the
document.form.submit() but this takes me through the validation
attached with the submit twice( giving me all the alerts twice).

Here's the code
function validate()
{

var validateVal = isValidated();//this guy does all the validation
if(!validateVal)
{
return false;
}
else
{
document.CustSearchFormBean.submit();
}

}

function checkKeyPressed()
{

if(window.event.keyCode=="13")
{

validate();
}
}

I call validate
a)for the enter button through <BODY onkeypress=checkKeyPressed();
b)for the mouseclick submit through <html:submit onclick=validate();


This may be a whole pointless workaround. Does Struts support
something more elegant?

Any solutions?

Thanks and Regards,
Megha.
 
M

Murray

Megha Vishwanath said:
Hi,

I have a struts form <html:form> in which although the focus rests on
the submit button, hitting the enter key does not submit the form.

I tried capturing the Enter key event and followed it by the
document.form.submit() but this takes me through the validation
attached with the submit twice( giving me all the alerts twice).

Here's the code
function validate()
{

var validateVal = isValidated();//this guy does all the validation
if(!validateVal)
{
return false;
}
else
{
document.CustSearchFormBean.submit();
}

}

function checkKeyPressed()
{

if(window.event.keyCode=="13")
{

validate();
}
}

I call validate
a)for the enter button through <BODY onkeypress=checkKeyPressed();
b)for the mouseclick submit through <html:submit onclick=validate();

No you don't need any of that ugliness. First, your validate() method needs
to return true or false. At the moment it's only returning false, or calling
form.submit (which is not necessary since the submit button does that for
you). Then the onclick should be onclick="return validate();" <-- note the
return.

However, the normal (and best?) way to do this is to use the form's onsubmit
event instead. Doing it this way also allows you to pass the form object to
the validate method e.g. onsubmit="return validate(this);" You can access
the form directly instead of using document.formName <-- you'd have to
change your validation code if you decided to rename your form bean in the
future.
 
Joined
Oct 2, 2009
Messages
1
Reaction score
0
HTML form does not submit in IE on Enter Key

Recently I came across posting which helped me to fix a similar problem.
Check it and see if this helps you...
forum.tutorials2learn.com/viewtopic.php?f=5&t=2
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top