简体   繁体   中英

How do I create my own confirm Dialog?

The confirm box only has two options: ok and cancel.

I'd like to make one myself, so I can add a third button: save and continue. But the issue I currently don't know how to solve, is that: once the custom confirm dialog is up, how do I block the previously running script (or navigation) from running? and then how do I make the buttons return values for the confirmation?

my understanding of the confirm dialog box is this: it's a visual boolean, that has the power to block navigation and scripts on a page. So, how do I emulate that?

如果您想要一个可靠的,可靠的解决方案...使用jQuery ...它将在每种浏览器上运行,而无需担心糟糕的IE等。http://jqueryui.com/demos/dialog/

In javascript, you don't stop while you're waiting for a user action : you set a callback (a function) that your dialog will call on close.

Here's an example of a small dialog library, where you can see how callbacks can be passed.

dialog = {};

dialog.close = function() {
    if (dialog.$div) dialog.$div.remove();
    dialog.$div = null;
};

// args.title
// args.text
// args.style : "", "error"  (optionnel)
// args.buttons : optional : map[label]->function the callback is called just after dialog closing
// args.doAfter : optional : a callback called after dialog closing
dialog.open = function(args) {
    args = args || {};
    if (this.$div) {
        console.log("one dialog at a time");
        return;
    }
    var html = '';
    html += '<div id=dialog';
    if (args.style) html += ' '+args.style;
    html += '><div id=dialog-title>'; 
    html += '</div>';
    html += '<div id=dialog-content>';
    html += '</div>';
    html += '<div id=dialog-buttons>';
    html += '</div>';
    html += '</div>';
    this.$div=$(html);
    this.$div.prependTo('body');
    $('#dialog-title').html(args.title);
    $('#dialog-content').html(args.text);
    var buttons = args.buttons || {'Close': function(){return true}};
    for (var n in buttons) {
        var $btn = $('<input type=button value="'+n+'">');
        $btn.data('fun', buttons[n]);
        $btn.click(function(){
            if ($(this).data('fun')()) {
                dialog.close();
                if (args.doAfter) args.doAfter();
            }
        });
        $btn.appendTo($('#dialog-buttons'));
    }
    this.$div.show('fast');
    shortcuts.on('dialog', {
        27: function(){ // 27 : escape
            dialog.close();
        }
    }); 
}

Two call samples :

dialog.open({
    title: 'ccccc Protection Error',
    text: 'There was an error related to cccc Protection. Please consult <a href=../cccc.jsp>this page</a>.',
    style: 'error'
});

var ok = false;
dialog.open({
        title: sometitle,
        text: someHtmlWithInputs,
        buttons: {
            'OK': function() {
                if (// inputs are valid) ok = true;
                return true;
            },
            'Cancel': function() {
                return true;
            }
        },
        doAfter: function() {
            if (ok) {
                if (newvg) {
                    cccmanager.add(vg);
                } else {
                    cccmanager.store();
                }
                if (doAfter) doAfter();
            } 
        }
    });

As specified by others, you may not need your own library if you just want to make a dialog.

You can make your own using javascript jquery and bootstrap. then you can add CallBack to any button you add save continue etc.

 function Delete() { Confirmation("Are You Sure ?",function(response){ if(response) { // continue alert("Confirmed"); } }); } function Confirmation(message, callback) { if ($('#modalConfirmation_MyTools') != undefined) $('#modalConfirmation_MyTools').remove(); $('body').append('<div class="modal" id="modalConfirmation_MyTools">\\ <div class="modal-dialog modal-sm">\\ <div class="modal-content">\\ <div class="modal-header">\\ <h3><span style="color:#f49e42;font-size:40px;" class="fa fa-exclamation-circle"></span> <span id="spanMessage_MyTools">&nbsp;&nbsp;</span></h3>\\ </div>\\ <div class="modal-footer">\\ <button type="button" data-dismiss="modal" class="btn btn-danger" id="btnConfirm_MyTools">Confirm</button>\\ <button type="button" data-dismiss="modal" class="btn btn-primary">Cancel</button>\\ </div>\\ </div>\\ </div>\\ </div>'); document.getElementById('spanMessage_MyTools').append(message); $('#modalConfirmation_MyTools').modal('toggle'); var confirmBtn = document.getElementById('btnConfirm_MyTools'); confirmBtn.onclick = function () {callback(true);} } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="text-center"> <br /><br /> <button type="button" class="btn btn-danger" onclick="Delete()">Delete</button> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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