简体   繁体   中英

HTML, Javascript, and JSP: Form not engaging in “action” when submit button is clicked

I have a "password_reset_status" JSP that is linked to by an e-mail that my web-app sends in response to a password reset request. The JSP is capable of displaying 1 of 3 things:

  • A form to enter a new password if the reset code in the link matches the one in the database
  • A message stating that the reset code is incorrect
  • A message stating that an error has occurred (no reset code for user, code expired, database connection problems, etc)

All of the code works, except the submission of the form. I have jQuery validation on the form that prevents submission until the input is valid. That works as its supposed to. However, when the input is valid (I know its valid because I have customized it to display checks and x's next to the valid and invalid fields, and the field attributes indicate they're valid), it doesn't execute its "action" attribute if its any javascript other than "alert()" (not even an alert with a string)!

I have a similar system for the other forms in my web app (the only differences are the fields that are to be validated, and the ajax function called to send the data) and they all behave as they should. However, they are all on a regular html page. Do you think the problem could be caused by the fact that this is a JSP?

Here's the code for the body of the JSP:

<!--The action normally contains an AJAX function that populates the text of "createNewPasswordStatusMessage", but the alert was put there for debugging purposes (it won't even launch the alert! -->


<body>

    <c:catch var="e">
        <c:choose>               
            <c:when test="${sessionScope.passwordResetStatus == applicationScope.passwordResetStates.passwordResetCodeCorrect}">
                <div id="content">
                    <h1 id="title"><!--Title of my web app--></h1>
                    <form id="createNewPasswordForm" action="javascript:alert(\"What is going on?\")" method="post">
                        <div class="formRows"> <label class="nameLabels" for="newPassword">New password:</label>        <input class="formFields" type="password" name="newPassword"/> </div>
                        <div class="formRows"> <label class="nameLabels" for="confirmNewPassword">Confirm:</label>      <input class="formFields" type="password" name="confirmNewPassword"/> </div>
                        <div class="formRows"> <input type="hidden" name="ID" value="${sessionScope.ID}"/></div>
                        <div class="formRows"> <input type="hidden" name="firstName" value="${sessionScope.firstName}"/></div>
                        <div class="formRows"> <input type="hidden" name="action" value="createNewPassword"/> </div>
                        <div class="formRows"> <input id="newPasswordFormSubmitButton" type="submit" value="Submit new password"/> </div>
                    </form>

                   <p id="createNewPasswordStatusMessage"></p>

                </div>
            </c:when>

            <c:when test="${sessionScope.passwordResetStatus == applicationScope.passwordResetStates.passwordResetCodeIncorrect}">
                <!-- Message stating that password is incorrect. Omitted for brevity. -->
            </c:when>
            <c:when test="${sessionScope.passwordResetStatus == applicationScope.passwordResetStates.passwordResetCodeExpired}">
                <!-- Message stating that reset code has expired or that an error may have occurred. Omitted for brevity. -->
            </c:when>
        </c:choose>
    </c:catch>
    <c:if test="${e!=null}">The caught exception is:
        <div id="content">
            <h1 id="title"><!--Title of my web app--></h1>
        <p id="statusMessage">"<c:out value="${e}" /></p>
        </div>
    </c:if>

</body>


I didn't want to inundate people with the head of the document, which contains the javascript (after the bug is worked out it will be put in to a seperate javascript file). The validation javascript works while a simple alert doesn't, so I think it may be something with the JSP code of the form. Can anyone point me in the right direction?

Edit

So to summarize:

  • action="(some url)" WORKS
  • action="alert()" WORKS (displays IP of server)
  • action="(any other javascript)" DOES NOT WORK

尝试使用添加处理程序

$('#createNewPasswordForm').submit(callback)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM