美好的一天,
以下是我的jsp代码:
<s:form beanclass="c.c.i.c.app.profile.ui.ChangePasswordAction" method="post" name="form1">
<!-- some others code here -->
<sx:row cssClass="button_row">
<sx:input name="updatePassword" image="submit"/>
</sx:row>
</s:form>
这是我的 jquery
:
<script type="text/javascript">
$(':input[name=updatePassword]').click(function() {
var answerProceed = confirm("Do you wish to proceed with the changes?");
if( answerProceed ){
var form = $("form[name=form1]");
form.attr("action", '<s:url beanclass="c.c.i.c.app.profile.ui.ChangePasswordAction" event="update"/>');
form.submit();
} else {
return false;}
});
</script>
我们可以看到,在 jQuery 中,它会触发 ChangePasswordAction
的 update
事件。
但是,目前我有一个问题,就是有时会触发2次,这种情况并不总是发生,但发生率约为50%。
以下是我从应用程序日志中获取的触发日志:
2019-06-26 18:19:13.658 [WebContainer : 31] TRACE o.s.w.c.s.XmlWebApplicationContext - [bmaker] - Publishing event in Root WebApplicationContext: org.springframework.security.event.authorization.AuthorizedEvent[source=FilterInvocation: URL: /common/change_password.html?update=&__stoken=1a47d3a9-29e8-4904-b204-3cb9fc0129f0]
2019-06-26 18:19:13.660 [WebContainer : 26] TRACE o.s.w.c.s.XmlWebApplicationContext - [bmaker] - Publishing event in Root WebApplicationContext: org.springframework.security.event.authorization.AuthorizedEvent[source=FilterInvocation: URL: /common/change_password.html?update=&__stoken=1a47d3a9-29e8-4904-b204-3cb9fc0129f0]
有人知道代码有什么问题吗?
最佳答案
如果 <input/>
是 type="submit"
您必须使用 preventDefault()
的形式如果您想在提交表格之前检查确认信息。
您可以通过e
作为监听器中的参数 Event
实现参数如 MouseEvent
(因为点击事件)在这种情况下。
例如:
$(':input[name=updatePassword]').click(function(e) { // <-- e implements Event
e.preventDefault(); // <-- prevent the submit of the form
var answerProceed = confirm("Do you wish to proceed with the changes?");
if (answerProceed) {
var form = $("form[name=form1]");
// var form = e.currentTarget.form; you can do this
// var form = this.form; or you can do this, "this" is implicit clicked element
form.attr("action", '<s:url beanclass="c.c.i.c.app.profile.ui.ChangePasswordAction" event="update"/>');
form.submit();
}
else {
return false;
}
});
您可以通过执行 .form
来访问元素的形式在元素上,请参阅
HTMLSelectElement.form
查看
关于java - form.submit() 将在表单内触发 double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56785843/