繁体   English   中英

Javascript如何在JSP上使用带有两个提交按钮的onSubmit确认对话框

[英]Javascript How to use Confirm Dialog Box onSubmit with two submit buttons on JSP

我的表单当前有两个提交按钮。 一个用于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;
  }
}

任何帮助,将不胜感激!

将“标记完成”按钮的onclick属性设置为此

 onclick="return confirm('Are you sure you want to continue')" 

并从表单中删除ConfirmComplete函数

您可以。 您可以像这样放置按钮

<input type="submit" value="Search" />
<input type="submit" value="Mark Complete" onclick="{return confirmComplete();}" />

单击“ 标记完成”按钮时,将调用confirmComplete函数,并且当用户在确认对话框中说“ 确定”时 ,将仅提交表单。

您需要通过单击按钮而不是表单提交来进行活动。 没有跨浏览器的方式可以知道提交表单的方式。

所以这是一个旁路解决方案:

<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>

当您调用“ confirm()” javascript弹出函数时,例如onclick =“ return Confirm('您确定要继续吗?')”时,确认框会出现并显示消息“您确定要继续吗?” 具有“ ok”和“ cancel”两个选项。单击“确定”时,返回true并继续执行网页;如果单击“取消”,则返回false并停止继续执行页面。

我不知道输入标签,但直接在按钮上单击事件对我来说不起作用。 就我而言,该表格会立即发布。

这是带有许多按钮的表单的可能解决方案(仅其中一个按钮必须显示确认消息)

在视图中:

<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(在外部文件中以供代码重用):

/**
* 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;
}

我很难做到这一点(需要大量的研究和测试),但是生成的代码非常简单。

默认情况下,我假设确认为是(如果单击的按钮不是用来显示消息的按钮,或者用户未提供有效的消息字符串)。

附加说明 :当然,如果用户浏览器阻止了客户端代码,则此代码将无法解决问题。

我希望它可以帮助某人,

蒙特利尔的JonathanParent-Lévesque

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM