2013-03-19 50 views
1

我已经使用引导模式让一个确认对话框这个下面的代码,它被假设是称为闭包内:为什么此Bootstrap确认对话框不起作用?

this.events.register(this, function(e) { 

    this.makeModal(e); 

    // this is what used to work for me 
    /* 
    if (confirm('Are you sure?')) { 
    alert(e.data); 
    } else { 
    // DO NOTHING 
    } 
    */ 
}); 

makeModal: function(e) {  

    //... 

    $(btnCancel).on("click", $(modal).modal('hide')); 
    $(btnConfirm).on("click", alert('confirm')); 
} 

只有两个按钮,分别是btnCancel和btnConfirm。问题在于调用外部函数后,内部警报也会立即触发,这使得确认对话框无效。我怎样才能解决这个问题? 由于特定的原因,我不能使用像引导箱等库。

或者我可以只返回是或否从这种模式?如果是的话我该怎么做?

预先感谢您!


编辑: 这是我可怜的企图是返回或没有:

makeModal: function(e) {  

    //... 
    var choice; 
    $(btnCancel).on("click", choice = false); 
    $(btnConfirm).on("click", choice = true); 
    return choice; 
} 

回答

3

这里是为了实现你正在试图做的另一种方式的例子:

$('#myModal').bind('show', function() { 
    var id = $(this).data('id'), 
    yesBtn = $(this).find('.danger'); 
    $(yesBtn).data('id', id); 

}).modal({ backdrop: true }); 

$('.delete-button').click(function(e) { 
    e.preventDefault(); 
    var id = $(this).data('id'); 
    $('#myModal').data('id', id).modal('show'); 
}); 

$('.danger').click(function(){ 
    var id = $(this).data('id'); 
    $('#myModal').modal('hide'); 
    alert('Deleting: ' + id); 
    //Do something with id 
}); 

Click here to see the JsFiddle

+0

我用其他方法来实现我的目标,所以我没有在我的应用程序中测试你的代码通货膨胀。但我会接受你的答案,因为小提琴的作品,谢谢你! – Hypnos 2013-03-28 00:11:15