Bootbox callback not working properly

2019-04-22 21:12发布

问题:

I'm trying to use this:

 $('#delete').live('click',function(){
                var result;
                bootbox.confirm("Are you sure?", function(response){
                        result=response;
                });
                alert(result);
                return result;
            });

But when the button is clicked:

Alert is shown first and only after that bootbox shows confirm dialog.

I want to return the response , but if i do it from within call back it doesn't work because it returns response from callback but not $('#devicedelete').live('click',function(){});

Btw i'm trying to submit a form basing on response. So if i return 'true' form will be submitted, else if it returns 'false', form wont be submitted.

Please check this: http://pastebin.com/9H3mxE9Y

I have one big table with checkboxes for each row, users select checkboxes and click on any of the buttons 'delete' , 'copy' etc and form should be submitted.

Thanks

回答1:

The dialog is async, you can prevent the default action right away, and then call the submit if the user accepts. Here is an example: http://jsfiddle.net/ty5Wc/3/

$('#delete').on('click', function (e) {
    e.preventDefault();
    bootbox.confirm("Are you sure?", function (response) {        
        if(response) {
            $('#form').submit();
        }
    });
});
$('#form').submit(function () {
    //do your validation or whatever you need to do before submit
});

Edit: After talking more with OP, he also wanted to pass button value in the query string, my solution for that is here: http://jsfiddle.net/ty5Wc/5/

$('#delete').on('click', function (e, confirmed) {
    if (!confirmed) {
        e.preventDefault();
        bootbox.confirm("Are you sure?", function (response) {
            if (response) {
                $('#delete').trigger('click', true);
            }
        });
    }
});
$('#form').submit(function (e) {
    //do your validation or whatever you need to do before submit
});