HTML、Javascript 和 JSP:不参与“操作”的表单当点击提交按钮时
我有一个“password_reset_status”JSP,通过我的网络应用程序为响应密码重置请求而发送的电子邮件链接到该JSP。 JSP 能够显示以下 3 项内容之一:
- 如果链接中的重置代码与数据库中的重置代码匹配,则用于输入新密码的表单
- 一条消息,说明重置代码不正确
- 一条消息,说明发生了错误(没有)为用户重置代码、代码过期、数据库连接问题等)
除提交表单外,所有代码均有效。我对表单进行了 jQuery 验证,在输入有效之前阻止提交。这就像它应该做的那样。但是,当输入有效时(我知道它有效,因为我已将其自定义为在有效和无效字段旁边显示检查和 x,并且字段属性指示它们有效),它不会执行其“操作”属性,如果它是除“alert()”之外的任何 JavaScript(甚至不是带有字符串的警报)!
我的网络应用程序中的其他表单有一个类似的系统(唯一的区别是要验证的字段,以及调用来发送数据的 ajax 函数),并且它们都按应有的方式运行。然而,它们都在常规的 html 页面上。您认为问题可能是由于这是 JSP 造成的吗?
下面是 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>
我不想用包含 javascript 的文档头部淹没人们(在解决 bug 后,它将被放入一个单独的 javascript 文件中) 。验证 javascript 可以工作,而简单的警报则不能,所以我认为这可能与表单的 JSP 代码有关。有人能指出我正确的方向吗?
编辑
总结一下:
- action="(some url)" WORKS
- action="alert()" WORKS(显示 IP 服务器)
- action =“(任何其他javascript)”确实 不工作
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加处理程序
尝试使用
$('#createNewPasswordForm').submit(callback)
Try adding your handler using
$('#createNewPasswordForm').submit(callback)