Announcement Announcement Module
Collapse
No announcement yet.
Form tag response to Enter keypress Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Form tag response to Enter keypress

    I'm pretty interested to know if anyone has encountered this strange behavior of the <form:input> tag with the Enter key press.

    Scenario:
    This is a typical spring view controller communication using validator, and onsubmit. When the user hits the enter key inside the input text box, the form is submitted as though the submit button was not pressed (which should be the default button). This was traced during onSubmit where request.getParameter("submitButton")==null returned true.
    on the other hand, if focus is on the page or anywhere outside the input boxes, hitting the enter key triggers the submit button to get pressed(request.getParameter("submitButton")==null returned false).

    Heres the tricky part, if there are more than 1 <form:input> tags (input boxes) , hitting the enter key inside any of the input boxes triggers the submitbutton to get pressed. This is quite interesting which brings me to wonder if anyone has ever come across this... This is something that has stood up as one of an uncanny bug on my project which I think has something to do with the way the tag <form:input path=...> is interpreted during form submission...

    if anyone has a resolution for this, your input will greatly be appreciated.

    Thanks.


    code
    <form:form commandName="transferform" name="transferform">
    <spring:hasBindErrors name="transferform">
    <div class="error">Please correct the following errors</div>
    <c:forEach items="${errors.allErrors}" var="error">
    <div class="errortext">- <spring:message message="${error}"/></div>
    </c:forEach>
    <br/>
    </spring:hasBindErrors>
    <table class="webform" cellpadding="0" cellspacing="0" border="0">
    <tr>
    <td class="label"><strong>source server</strong></td>
    </tr>
    <tr>
    <td class="bottom"><form:select path="sourceServer" onchange="javascript:changeSourceServer()"><form:o ptions items="${sourceServers}" itemLabel="name" itemValue="id"/></form:select> <form:select path="courtroom"><form:options items="${courtrooms}" itemLabel="name" itemValue="id"/></form:select></td>
    </tr>
    <tr>
    <td class="label"><strong>destination server</strong></td>
    </tr>
    <tr>
    <td class="bottom"><form:select path="destinationServer"><form:options items="${destinationServers}" itemLabel="name" itemValue="id"/></form:select></td>
    </tr>
    <tr>
    <td class="label"><strong>date</strong></td>
    </tr>
    <tr>
    <td class="bottom">
    <table cellpadding="0" cellspacing="0" border="0">
    <tr>
    <td><form:input path="transferDateAsString" size="12"/></td>
    <td width="100%">&nbsp;</td>
    </tr>
    <tr>
    <td align="center">dd/mm/yyyy</td>
    <td width="100%">&nbsp;</td>
    </tr>
    </table>
    </td>
    </tr>
    <tr>
    <td colspan="6"><input type="submit" name="submitButton" value="Submit" /> &nbsp; <input type="submit" name="submitButton" value="Cancel" onclick='return confirm("Are you sure you want to cancel?");'/></td>
    </tr>
    </table>
    </form:form>

  • #2
    very misleading

    Turns out the cause of the problem was a missing javascript line of code.

    a very simple return false;

    it wastn't mentioned on the source that was supplied but for those who might encounter this same bewildering misfortune. Here is the javascript that triggers the keypress:

    <script language="JavaScript" type="text/javascript">
    document.onkeypress = processKey;

    function processKey(e)
    {
    if (null == e)
    e = window.event ;
    if (e.keyCode == 13) {
    document.forms[0].submitButton[0].click();
    return false;
    }
    }
    </script>

    I didn't have the return false; code when i posted this call for help. But then again I wasn't expecting any return to happen... bugger me.

    Moral of the story, know the behavior of the script... or die debugging!

    Cheers!

    Comment

    Working...
    X