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

Discussion in 'Java' started by Megha Vishwanath, Jul 18, 2004.

  1. 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.
     
    Megha Vishwanath, Jul 18, 2004
    #1
    1. Advertising

  2. Megha Vishwanath

    Murray Guest

    "Megha Vishwanath" <> wrote in message
    news:...
    > 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.
     
    Murray, Jul 18, 2004
    #2
    1. Advertising

  3. Megha Vishwanath

    tutorialsmaster

    Joined:
    Oct 2, 2009
    Messages:
    1
    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
     
    tutorialsmaster, Oct 2, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    2
    Views:
    3,233
    bruce barker
    May 11, 2004
  2. Chinnala
    Replies:
    3
    Views:
    479
    Gaurav Vaish \(www.Edujini-Labs.com\)
    Jan 10, 2007
  3. Replies:
    4
    Views:
    992
  4. BrianDH

    Enter Key H*ll, control enter key submit button

    BrianDH, Jan 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    429
    Anders Matthiessen
    Jan 12, 2005
  5. Megha Vishwanath
    Replies:
    3
    Views:
    788
    Megha Vishwanath
    Jul 20, 2004
Loading...

Share This Page