我正在尝试使用java脚本基于确认条件提交弹簧形式。 以下是我的控制器
@RequestMapping("/loadPage.do")
public ModelAndView loadPage()
{
ModelAndView modelAndView;
//some code
return modelAndView;
}
@RequestMapping(value="/submitAction.do" , method=RequestMethod.POST)
public String submitForm(@ModelAttribute Object form, Model m ){
//some code
return "page";
}
JSP
<script>
function confirmForChanges (){
var r= confirm("Do you want to proceed");
if (r == true) {
document.getElementById('submitButton').action = "/root/submitAction.do";
document.getElementById('submitButton').submit();
alert("Your changes have been saved");
}if (r ==false){
alert("changes not saved")
}
}
</script>
<form:form action="/submitAction.do" commandName="command" method="post">
<input id=cancelButton type="button" value="Cancel" />
<input id=submitButton type="submit" value="Submit" onclick="javascript:confirmForChanges();"/>;
</form:form>
问题是,即使我取消,表单提交:(
我尝试在jsp中删除表单中的action="/submitAction.do"
,但没有运气。
答案 0 :(得分:4)
尝试使用活动onsubmit="return confirmForChanges();"
function confirmForChanges() {
var save = confirm("Do you want to proceed");
alert(save ? 'Your changes have been saved' : 'changes not saved');
return save; //if true then submit else don't submit
}
<form:form action="/submitAction.do" commandName="command" method="post" onsubmit="return confirmForChanges();">
<input id=cancelButton type="button" value="Cancel" />
<input id=submitButton type="submit" value="Submit" />
</form:form>