JavaScript confirm box with custom buttons

后端 未结 3 437
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 23:05

Can I write a custom confirm box in JavaScript that, instead of the default OK and CANCEL button, shows a SAVE and DELETE

相关标签:
3条回答
  • 2020-12-18 23:28

    Use the jQuery UI Dialog Box for this.

    You can do something like this:

    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>

    0 讨论(0)
  • 2020-12-18 23:28
    $('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.

    0 讨论(0)
  • 2020-12-18 23:38

    Not with the native JavaScript confirm box. You could use one of the many JavaScript UI components that emulate modal dialogs to do this instead.

    Here's an example: https://jqueryui.com/dialog/#modal-confirmation

    I've never used this so use at your own risk.

    0 讨论(0)
提交回复
热议问题