简体   繁体   English

jQuery在模式框中对服务器的多个请求

[英]jQuery multiple requests to the server in modal box

I have a problem and don't know how to solve it. 我有一个问题,不知道如何解决。

HTML Write Message HTML写消息

<a href="javascript:void(0);" data-name="Test1 (Expert)" data-to="2" class="btn btn-default btn-sm message"><i class="fa fa-envelope"></i> Write Message</a>

<!--Modal-->
<div class="modal fade" id="msg_modal" tabindex="-1" role="dialog" aria-labelledby="msg_modal">
...
   <a href="javascript:void(0);" class="btn btn-success pull-right send_msg"><i class="fa fa-plus"></i> Send Message</a>
</div>

JS JS

$('.message').on('click', function(e){
   $('#msg_modal').modal('show').on('shown.bs.modal', function(e) {
     $.ajax({
       ...
     });
   });
});

My problem: When I click on .message selector - ajax loads data one time; 我的问题:当我单击.message选择器时-ajax加载数据一次; When I close dialog window and click again on .message selector - ajax loads multiple time. 当我关闭对话框窗口并再次单击.message选择器时,ajax将加载多个时间。 How can I solve this? 我该如何解决? Thanks. 谢谢。

Seems like every time you click the button, it will register new listener again and again. 好像每次单击按钮一样,它将一次又一次注册新的侦听器。 Try separating .modal('show') with event shown.bs.modal like so : 尝试将.modal('show')与事件shown.bs.modal如下所示:

$('.message').on('click', function(e){
  $('#msg_modal').modal('show');
});

$('#msg_modal').on('shown.bs.modal', function(e) {
   $.ajax({
    ...
  });
});

You only need to register the shown.bs.modal listener once as shown.bs.modal listener will trigger automatically when .modal('show') called. 您只需要注册一次shown.bs.modal监听器即可,如图所示shown.bs.modal监听器将在.modal('show')时自动触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM