[英]JSP: Confirm before submit
I'm trying to show a confirm dialog before submitting form data. 我正在尝试在提交表单数据之前显示确认对话框。 I want a dialog to pop up with ok and cancel where ok submits the data and cancel does nothing.
我想要一个对话框弹出ok和取消,其中ok提交数据,取消什么都不做。 This has proven to be trickier than I ever could have imagined.
事实证明,这比我想象的要复杂得多。
The web application is in java and jsp. Web应用程序使用java和jsp。 I've tried the following using "onclick=" hoping for a simple solution to no avail:
我尝试了以下使用“onclick =”希望一个简单的解决方案无济于事:
Java: ReceiveAction.java Java:ReceiveAction.java
public Resolution receiveInventory() {
...
return new ForwardResolution(ADJUSTMENT);
}
jsp: receive-action.jsp jsp:receive-action.jsp
<button name="_eventName" value="receiveInventory" type="submit" class="btn btn-green disabler-ignore-readonly" onclick="javascript:return confirm('are you sure?');">Receive</button>
The problem is I can get the confirm dialog to appear with the above code but receiveInventory is triggered whether I hit ok or cancel. 问题是我可以使用上面的代码显示确认对话框,但是无论我点击确定还是取消都会触发receiveInventory。 I need it to only trigger when I hit ok.
我需要它只在我点击确定时触发。 I feel like I'm very well versed in java and jsp but I'm less than a novice when it comes to javascript(I avoid it like the plague) and I think that's what I'm going to need to pull this off.
我觉得自己非常精通java和jsp,但是当谈到javascript时我不是新手(我就像瘟疫一样避免它)而且我认为这就是我需要解决的问题。 Any help or pointing in the right direction would be greatly appreciated.
任何帮助或指向正确的方向将不胜感激。
Your button is a "submit" type so it is most likely triggering the form's onSubmit
event when it is clicked, regardless of your own click handler. 您的按钮是“提交”类型,因此无论您自己的点击处理程序如何,它都很可能在单击时触发表单的
onSubmit
事件。 Also, confirm returns a boolean which you can use to tell if they selected the "Ok" button. 此外,确认返回一个布尔值,您可以使用它来判断他们是否选择了“确定”按钮。
function checkSubmission() {
let confirmed = confirm("Are you sure?");
if (confirmed) {
// Submit your form....
recieveInventory(); // document.getElementById('myFormId').submit();
} else {
// Handle cancel here if needed
}
}
<button name="_eventName" value="receiveInventory" type="button" class="btn btn-green disabler-ignore-readonly" onclick="javascript:return checkSubmission();">Receive</button>
Ideally you probably should just set your form's submit
handler to call checkSubmission
. 理想情况下,您可能只需将表单的
submit
处理程序设置为调用checkSubmission
。 Then you don't have to set a click
handler on the button at all 然后,您根本不必在按钮上设置
click
处理程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.