jquery模态函数的双触发器

时间:2016-06-26 06:05:30

标签: jquery bootstrap-modal

我有这个代码,它总是使前一个函数加倍。

var deleteModal = $('.bs-delete-modal-sm');
deleteModal.on('shown.bs.modal', function(event) {
  var button = $(event.relatedTarget); // Button that triggered the modal
  var recipientId = button.data('aishid');

  var confirmBtn = deleteModal.find('.confirmDeleteBtn');
  var parent = button.closest('.year-calendar');
  //console.log(confirmBtn);
  confirmBtn.click(deleteHoliday);

});

function deleteHoliday() {
  $('.textherearea').append('here ');
  deleteModal.modal('hide');
}
<html>

<head>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

</head>

<body>
  <button class="deleteBtn" data-toggle="modal" data-target=".bs-delete-modal-sm" data-aishid="24"><i class="fa fa-trash"></i>
  </button>
  <button class="deleteBtn" data-toggle="modal" data-target=".bs-delete-modal-sm" data-aishid="25"><i class="fa fa-trash"></i>
  </button>
  <div class="modal fade bs-delete-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
    <div class="modal-dialog modal-sm">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
          </button>
          <h4 class="modal-title" id="myModalLabel">Confirm delete?</h4>
        </div>
        <div class="modal-body">
          Delete this holiday?
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary confirmDeleteBtn">Confirm</button>
        </div>
      </div>
    </div>
  </div>
  <div class='textherearea'></div>
</body>

</html>
似乎在模态隐藏并通过按钮触发器重新启动后,确认按钮附加功能运行两倍。

有人可以指出我如何解决这个问题,并让它只运行一次。

感谢。

1 个答案:

答案 0 :(得分:1)

在JQuery中添加事件处理程序不会删除或删除现有的事件处理程序,每次使用confirmBtn.click(deleteHoliday);时,JQuery都会向现有处理程序列表添加一个单击处理程序,当用户单击它时,JQuery会触发所有处理程序它们一个接一个,所以你需要解除绑定或关闭你绑定到按钮的现有点击事件,然后再绑定新的点击事件:

confirmBtn.unbind( "click" ); //unbind is deprecated and its better to use .off()
//
confirmBtn.off( "click" );

您还可以使用以下方法检查confirmBtn是否分配了任何点击事件:

 var btnEvents = $._data(confirmBtn[0], 'events');
 if(btnEvents && btnEvents.click)
    console.log("btn has click event");
 else
    console.log("btn hasn't click event");

&#13;
&#13;
var deleteModal = $('.bs-delete-modal-sm');
deleteModal.on('shown.bs.modal', function(event) {
  var button = $(event.relatedTarget); // Button that triggered the modal
  var recipientId = button.data('aishid');

  var confirmBtn = deleteModal.find('.confirmDeleteBtn');
  var parent = button.closest('.year-calendar');
  //console.log(confirmBtn);
   var btnEvents = $._data(confirmBtn[0], 'events');
   console.log(btnEvents);
  if(btnEvents && btnEvents.click)
    console.log("btn has click event");
  else
    console.log("btn hasn't click event");
  //confirmBtn.unbind( "click" );
  confirmBtn.off( "click" );
  confirmBtn.click(deleteHoliday);

});

function deleteHoliday() {
  $('.textherearea').append('here ');
  deleteModal.modal('hide');
}
&#13;
<html>

<head>
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" />
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

</head>

<body>
  <button class="deleteBtn" data-toggle="modal" data-target=".bs-delete-modal-sm" data-aishid="24"><i class="fa fa-trash"></i>
  </button>
  <button class="deleteBtn" data-toggle="modal" data-target=".bs-delete-modal-sm" data-aishid="25"><i class="fa fa-trash"></i>
  </button>
  <div class="modal fade bs-delete-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
    <div class="modal-dialog modal-sm">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
          </button>
          <h4 class="modal-title" id="myModalLabel">Confirm delete?</h4>
        </div>
        <div class="modal-body">
          Delete this holiday?
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          <button type="button" class="btn btn-primary confirmDeleteBtn">Confirm</button>
        </div>
      </div>
    </div>
  </div>
  <div class='textherearea'></div>
</body>

</html>
&#13;
&#13;
&#13;