Javascript 引导模式方法多次激发

Javascript 引导模式方法多次激发,javascript,jquery,bootstrap-modal,Javascript,Jquery,Bootstrap Modal,我面临引导模式的问题。 我已经实现了下面的代码。 我不知道发生了什么,但第一次单击“是”按钮时,它工作正常,但当我单击“否”按钮时,以及之后单击“是”按钮时,会多次触发“是”按钮单击事件。 我不知道为什么会这样 $('#confirmationModal').modal('show'); $("#confirmationModal").on('shown.bs.modal', function() { $('#confirmationModal').modal({ backdrop:

我面临引导模式的问题。 我已经实现了下面的代码。 我不知道发生了什么,但第一次单击“是”按钮时,它工作正常,但当我单击“否”按钮时,以及之后单击“是”按钮时,会多次触发“是”按钮单击事件。 我不知道为什么会这样

$('#confirmationModal').modal('show');
$("#confirmationModal").on('shown.bs.modal', function() {
  $('#confirmationModal').modal({
    backdrop: 'static',
    keyboard: false
  }).one('click', '#yesbutton', function(e) {
    $('#confirmationModal').modal('hide');

    //Doing Ajax Stuff and alert data.    

    //Very important line.
    //This is needed to prevent multiple firing of the yes button event.

    $(this).off(event);

  })
}); * * strong text * *

我认为问题在于事件被多次注册,因为在表的每一行的另一个click事件上调用了上述代码


因此,我通过为yesno按钮显式注册单击事件,并仅调用modal.show()来显示确认对话框,从而解决了这个问题。

您能否添加代码片段以更好地理解?$(“#confirmationModal”).on('show.bs.modal',function(e){e.peventDefault()$(“#confirmationModal”).modal({background:'static',keyboard:false}).one('click',“#yesbutton',function(e){$('#confirmationModal').modal('hide');//执行Ajax内容和警报数据//非常重要的一行//这是防止多次触发yes按钮事件所必需的。$(This).off(event);});**强文本**使用event.preventDefault()