Javascript 如何在 JSP 上使用带有两个提交按钮的确认对话框 onSubmit
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15580167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript How to use Confirm Dialog Box onSubmit with two submit buttons on JSP
提问by Richard
My form currently has two submit buttons. One for Search
and the other for a Mark Complete
function. I need to show a confirm dialog box ONLY when the "Mark Complete" button is clicked to submit the form with that validation is passed. Is it possible to identify this? I currently have the below confirmComplete
function:
我的表单目前有两个提交按钮。一个用于函数Search
,另一个用于Mark Complete
函数。仅当单击“标记完成”按钮提交表单并通过验证时,我才需要显示确认对话框。是否有可能识别这一点?我目前有以下confirmComplete
功能:
function confirmComplete() {
alert("confirmComplete");
var answer=confirm("Are you sure you want to continue");
if (answer==true)
{
return true;
}
else
{
return false;
}
}
Any help would be appreciated!
任何帮助,将不胜感激!
回答by Carlos Blanco
Set the onclick
attribute of the "Mark Complete" button to this
将onclick
“标记完成”按钮的属性设置为此
onclick="return confirm('Are you sure you want to continue')"
and remove the confirmComplete function from the form
并从表单中删除 confirmComplete 功能
回答by Ravindra Gullapalli
You can. You can put your buttons like this
你可以。你可以把你的按钮像这样
<input type="submit" value="Search" />
<input type="submit" value="Mark Complete" onclick="{return confirmComplete();}" />
When Mark Completebutton is clicked then the confirmComplete
function will be called and when user says OKin the confirm dialog then only the form will be submitted.
当马克完全按下按钮然后在confirmComplete
函数将被调用,当用户说OK的确认对话框则只有表格将被提交。
回答by Adidi
So here is a bypass solution:
所以这是一个绕过解决方案:
<form id="frm" action="page.php" method="post" onsubmit="return onSubmit();">
<input />
<input type="submit" value="sub1" onclick="sub1();" />
<input type="submit" value="sub2" onclick="sub1();" />
</form>
<script type="text/javascript">
<!--
var frm = document.getElementById('frm');
function onSubmit(){
return false;
}
function sub1(){
alert('s1');
frm.submit();
}
function sub2(){
alert('s2');
}
//-->
</script>
回答by epascarello
You need to do the event from the click on the button and not the form submission. There is no crossbrowser way to know what submitted the form.
您需要通过单击按钮而不是表单提交来执行该事件。没有跨浏览器的方式来知道提交表单的内容。
回答by vishal kadam
when you call "confirm()" javascript pop up function , like onclick="return confirm('Are you sure to proceed?')" ,confirm box get appear with message 'Are you sure to proceed?' with two options 'ok' and 'cancel'.when you click ok it return true and proceed further execution of web page or if you click cancel it will return false and stop further execution of web page.
当您调用“confirm()”javascript 弹出函数时,例如 onclick="return confirm('Are you sure to continue?')" ,确认框出现并显示消息“您确定继续吗?” 有两个选项“确定”和“取消”。当您单击确定时,它返回真并继续执行网页,或者如果您单击取消,它将返回假并停止网页的进一步执行。
回答by Jonathan Parent Lévesque
I don't know for an input tag, but it didn't worked for me with the click event directly on a button. In my case, the form was posted right away.
我不知道输入标签,但它对我直接在按钮上的点击事件不起作用。就我而言,表格立即发布。
Here's a possible solution for a form with many buttons (for which only one of them must display the confirmation message)
这是具有许多按钮的表单的可能解决方案(其中只有一个必须显示确认消息)
In the view:
在视图中:
<FORM name="F_DESTINATION_DB" id="F_DESTINATION_DB" method="POST" onsubmit="return popConfirmationBox('<?php echo LanguageControler::getGeneralTranslation("DELETE_CONFIRMATION_MESSAGE", "Deleting is an irreversible action. Are you sure that you want to proceed to the deleting?");?> ','DELETE_DB_BUTTON')">
Javascript (in external file for code reuse):
Javascript(在外部文件中用于代码重用):
/**
* Display a confirmation message box to validate if we must post the page or not.
*
* @param message String to display
* @param tagId String id of the tag that must display the message.
*
* @return Boolean (confirmation)
*/
function popConfirmationBox(message, tagId){
var confirmation = true;
if (typeof tagId === 'string' && document.activeElement.id.toUpperCase() === tagId.toUpperCase()) {
if (typeof message === 'string' && message.length > 0) {
confirmation = window.confirm(message);
}
}
return confirmation;
}
I had quite a hard time to achieve this (needed lot of research and testing), but the resulting code is pretty simple.
我很难做到这一点(需要大量的研究和测试),但生成的代码非常简单。
By default, I assume that the confirmation is yes (in case the clicked button is not the one meant to display the message or if the user doesn't supply a valid message string).
默认情况下,我假设确认为是(如果单击的按钮不是用于显示消息的按钮,或者如果用户没有提供有效的消息字符串)。
Additional note: Of course, this code won't do the trick if the user browser block client side code.
附加说明:当然,如果用户浏览器阻止客户端代码,此代码将不起作用。
I hope it will help someone,
我希望它会帮助某人,
Jonathan Parent-Lévesque from Montreal
来自蒙特利尔的 Jonathan Parent-Lévesque