我想使用Bootstrap Modal按钮返回click事件的结果

时间:2017-01-19 09:15:52

标签: javascript jquery

我的目标是用`function

覆盖jquery confirm()函数
confirm(message){
        //There is a modal opened already
        $('#confirm').modal('show', {backdrop: 'static'}); //Show second modal on top of the first modal
        $('#confirm').find('.modal-title').html(message);
                $('.confirm').on('click',function(){ //on Confirm button click of the second modal
                        $('#confirm').removeClass('fade').modal('hide');//Hide the Second modal
                        $('.page-body').addClass('modal-open'); // Addig back modal-open class to body element to prevent modal conflict
                        callback(); //some suggested this but it doesnt work
                        return true; //I want to return this to the function called this function but i get no results
                });
                $('.cancel').on('click',function(){
                        $('#confirm').removeClass('fade').modal('hide');
                        $('.page-body').addClass('modal-open'); 
                        callback(); 
                        return false; 
                });

            $('.close').on('click',function(){
                    $('#confirm').removeClass('fade').modal('hide');
                    $('.page-body').addClass('modal-open'); 
                    callback(); 
                    return 'cancel';
            });
}`  I am not getting any results when I call the function

1 个答案:

答案 0 :(得分:2)

  1. jQuery没有confirm()功能,因此您要么使用浏览器的内置功能,要么使用某个插件...

  2. 您没有从自定义confirm功能返回任何内容。您正在定义最终可能返回值的事件处理程序,但在调用之前它们不会返回任何内容。

  3. 很难弄清楚这个功能应该做什么。你能用英语概括逻辑吗?

    以下是您的代码现在正在执行的操作:

    当用户调用内置confirm函数时,

    1. 显示模态对话框

    2. 将其标题设置为message参数

    3. 立即执行任何操作,但将onclick事件处理程序附加到confirm类。

    4. 立即执行任何操作,但将onclick事件处理程序附加到cancel类。

    5. 立即执行任何操作,但将onclick事件处理程序附加到close类。

    6. 在那时对那些事件处理程序做任何有用的事情为时已晚,而且你永远不会看到它们的返回值。

      如果您发布HTML并逐步说明,详细您希望事件发生的顺序,那么人们可以提供帮助。但除此之外,我不确定如何。