简体   繁体   English

带有自定义按钮的 JavaScript 确认框

[英]JavaScript confirm box with custom buttons

我可以在 JavaScript 中编写一个自定义确认框,而不是默认的OKCANCEL按钮,而是显示SAVEDELETE按钮吗?

Use the jQuery UI Dialog Box for this.为此使用jQuery UI 对话框

You can do something like this:你可以这样做:

JS: JS:

 $(function() { $("#dialog-confirm").dialog({ resizable: false, height: "auto", width: 400, modal: true, buttons: { "Do it": function() { $(this).dialog("close"); }, Cancel: function() { $(this).dialog("close"); } } }); });
 <link href="https://jqueryui.com/jquery-wp-content/themes/jquery/css/base.css" rel="stylesheet"/> <link href="http://jqueryui.com/jquery-wp-content/themes/jqueryui.com/style.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <div id="dialog-confirm" title="Is it treason, then?"> <p><span class="ui-icon ui-icon-alert" style="float:left; margin:12px 12px 20px 0;"></span>Am I the Senate?</p> </div>

Not with the native JavaScript confirm box.不适用于本机 JavaScript 确认框。 You could use one of the many JavaScript UI components that emulate modal dialogs to do this instead.您可以使用模拟模态对话框的众多 JavaScript UI 组件之一来执行此操作。

Here's an example: https://jqueryui.com/dialog/#modal-confirmation这是一个例子: https : //jqueryui.com/dialog/#modal-confirmation

I've never used this so use at your own risk.我从未使用过这个,所以使用风险自负。

$('confirm text').dialog(
    {
        modal:true, //Not necessary but dims the page background
        buttons:{
            'Save':function() {
                //Whatever code you want to run when the user clicks save goes here
             },
             'Delete':function() {
                 //Code for delete goes here
              }
        }
    }
);

This should work, but you will need to download or link to jQuery and the jQuery UI libraries to run this.这应该可以工作,但您需要下载或链接到 jQuery 和 jQuery UI 库才能运行它。

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

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