简体   繁体   English

如何防止在 jQuery 中多次加载文件

[英]How to prevent load file multiple times in jQuery

Might my subject of this thread is same of multiple threads on stackoverflow but please believe me I have been read all the threads on this platform and and over the google but yet I not get the expected answer of my question below- When I clicking the button first time the modal div load the URL properly but after closing the modal div and hit the button to open again it load twice.我的这个线程的主题可能与 stackoverflow 上的多个线程相同,但请相信我,我已经阅读了这个平台上和谷歌上的所有线程,但我没有得到我下面问题的预期答案 - 当我点击按钮时第一次模态 div 正确加载 URL,但在关闭模态 div 并点击按钮再次打开后,它加载了两次。 means at every button click the modal load URL twice of previous load URL.意味着在每个按钮上单击两次先前加载 URL 的模式加载 URL。 Suppose if this time LOAD URL load 2 time next time its load 4 times and so on..假设这次 LOAD URL 加载 2 次,下次加载 4 次,依此类推。

Even, I used return false but I don't the get the answer , I also read the answer of other thread but it not matching to my question codes.甚至,我使用return false但我没有得到答案,我也阅读了其他线程的答案,但它与我的问题代码不匹配。

Note: Please I am rules of stackoverflow policy and not making this thread after read all thread and not get answer then, Please not mark this Duplicate / Under Pending and any negative marks.注意:请我是 stackoverflow 策略的规则,在阅读所有线程后不创建此线程,然后没有得到答案,请不要标记此重复/待处理和任何负面标记。 Where I am doing wrong in my code I am new in this Debug.我在代码中做错的地方我是这个调试的新手。

 $('#reveal_AddSenderMod').on('click', function() { $('.modal.fade.modal-style2').on('shown.bs.modal', function() { $(this).find('.modal-body').find('#loadURL').load('./loadPage.html').fadeIn('slow'); return false; }); })
 <link rel="stylesheet" href="http://shashani-humanth.github.io/Notebook-AdminPanel/css/bootstrap.css" type="text/css" /> <button type="button" id="reveal_AddSenderMod" data-toggle="modal" data-target="#modal-style2" style="width:75px; display: block;margin: 0 auto;" data-keyboard="false" data-backdrop="static">OPEN MODEL</button> <div class="modal fade modal-style2 hidden-print" id="modal-style2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"></div> <div class="modal-body"> <div class="row"> <div id="loadURL" class="animated fadeIn"></div> <button data-dismiss="modal">CLOSE MODEL</button> </div> </div> </div> </div> </div> <script> $(document).on("click", "button[data-dismiss='modal']", function(e){ e.preventDefault(); $('div.modal-body').find('div#loadURL').find('div.senderIDAdd_module').empty(); //remove() is also not works }); </script> <script src="http://shashani-humanth.github.io/Notebook-AdminPanel/js/jquery.min.js"></script> <!-- Bootstrap --> <script src="http://shashani-humanth.github.io/Notebook-AdminPanel/js/bootstrap.js"></script>

You are binding event muilple time.您正在绑定事件多次。

On click, you should just open modal.单击时,您应该只打开模态。 not bind.不绑定。 Bind should be done one time.绑定应该做一次。

$('#reveal_AddSenderMod').on('click', function() {
  $('.modal.fade.modal-style2').modal('show');
})

$('.modal.fade.modal-style2').on('shown.bs.modal', function() {
    $(this).find('.modal-body').find('#loadURL').load('./loadPage.html').fadeIn('slow');
    return false;
  });

//Some dummy code //一些虚拟代码

$('body').on('click', '#on-submit-senderid', function() {
        if(localStorage.getItem("review_submitte")) {
          return;
        }
        if(!$('input[name="sender_id_confirm"]').is(':checked')) {
            mkNoti(['Ops!'],['Please agree the condition to get Custom Sender ID'],{ sound: true, status:['danger'],dismissable: false });
            return;
        } else {
            $.ajax({
                            ...
                success: function(response) {
                    if (response.status == 'success') {
                        //SUCCESS
                        return false;
                    } else {
                        mkNoti([response.title],[response.message],{ sound: true, status:[response.status],dismissable: false });
                        return false;
                    }
                    localStorage.setItem("review_submitte", "true")
                }
            });
            hide_loader();
            return false;
        }
    });

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

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