gpt4 book ai didi

java - 使用 Javascript 提交 Spring 表单

转载 作者:行者123 更新时间:2023-11-30 07:05:45 27 4
gpt4 key购买 nike

我正在尝试使用 java 脚本提交基于确认条件的 spring 表单。以下是我的 Controller

@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" 但没有成功。

最佳答案

尝试使用事件 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>

关于java - 使用 Javascript 提交 Spring 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26376369/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com