为什么此Bootstrap确认对话框不起作用?

时间:2013-03-19 23:43:11

标签: twitter-bootstrap

我有以下代码用于使用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。问题是在调用外部函数之后,内部警报也会立即被触发,这会使确认对话框无效。我怎样才能解决这个问题? 由于特定原因,我不能使用像bootbox等的库。

或者我可以从此模式中返回是或否?如果是,我该怎么办?

提前谢谢!


编辑: 这是我努力回归是或否:

makeModal: function(e) {    

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

1 个答案:

答案 0 :(得分: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