如何使用 MS Word 中显示的三个按钮“是”、“否”和“取消”来显示确认警报

IT技术 javascript asp.net
2021-03-14 19:54:58

我正在通过 JavaScript 显示一个确认警告框:

function checked() {
 if (hdnval.toLowerCase() != textbox1.toLowerCase()) {
  var save = window.confirm('valid')
   if (save == true) 
   {
     return true;
   }
   else
    {
      return false;
    }
 }
}

确认警报显示有两个按钮:确定和取消。

我想在我的确认提醒中显示第三个按钮。我希望三个按钮是这样的:“是”“否”“取消”,正如它在 MS Word 中显示的那样。

2个回答

这不能用原生的 javascript 对话框来完成,但很多 javascript 库都包含更灵活的对话框。为此,您可以使用jQuery UI 的对话框之类的东西

另请参阅这些非常相似的问题:

这是一个示例,如jsFiddle 所示

<html><head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/normalize.css">
    <link rel="stylesheet" type="text/css" href="/css/result-light.css">
    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/themes/base/jquery-ui.css">
</head>
<body>
    <a class="checked" href="http://www.google.com">Click here</a>
    <script type="text/javascript">

        $(function() {
            $('.checked').click(function(e) {
                e.preventDefault();
                var dialog = $('<p>Are you sure?</p>').dialog({
                    buttons: {
                        "Yes": function() {alert('you chose yes');},
                        "No":  function() {alert('you chose no');},
                        "Cancel":  function() {
                            alert('you chose cancel');
                            dialog.dialog('close');
                        }
                    }
                });
            });
        });

    </script>
</body><html>
@Rocky:如果我有时间,我可能会提供一个代码示例,但 jQuery 和 jQuery UI 对话框的文档都非常清楚。查看我链接到的演示页面的源代码。使用 Google 查找教程。当我的几分钟工作可以为您节省数小时的精力时,我喜欢提供帮助,但我不会为您完成所有工作。
2021-04-27 19:54:58
@Rocky:您现在在每个按钮上都有一个处理程序,用于确定单击“是”、“否”或“取消”时会发生什么。使用这些处理程序根据用户点击的内容实际发生一些事情。不是返回truefalse让调用函数决定在每种情况下做什么,您可以让调用函数传入回调函数来确定当用户单击每个按钮时会发生什么。
2021-04-28 19:54:58
我对 jquery 一无所知,请您提供更多演示说明。表明我可以使用 jquery 并且可以替换我的代码..请
2021-05-03 19:54:58
@ StriplingWarrior:提供一些帮助和示例代码对我来说就足够了,如果我有一些想法,我可以做的一切,非常感谢您的回复和回答。
2021-05-06 19:54:58
非常感谢,假设我想返回 true 或 false 那么我需要做什么?
2021-05-07 19:54:58

如果您不想使用单独的 JS 库为此创建自定义控件,则可以使用两个confirm对话框进行检查:

if (confirm("Are you sure you want to quit?") ) {
    if (confirm("Save your work before leaving?") ) {
        // code here for save then leave (Yes)
    } else {
        //code here for no save but leave (No)
    }
} else {
    //code here for don't leave (Cancel)
}
最简单的解决方案始终是我的最爱!
2021-04-25 19:54:58