Bootbox callback not working properly

给你一囗甜甜゛ 提交于 2019-12-10 03:54:19

问题


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
});


来源:https://stackoverflow.com/questions/16759727/bootbox-callback-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!