简体   繁体   English

如何使用动态内容和选择器使jquery事件在引导模式中工作?

[英]How do I make jquery events work in a bootstrap modal with dynamic content and selectors?

I've spent a lot of time on this and browsed through reams of SO posts without success. 我花了很多时间在这上面浏览了大量的SO帖子而没有成功。 I know I'm missing something obvious. 我知道我错过了一些明显的东西。 I'm not experienced with javascript or jquery, so please answer in detail. 我对javascript或jquery没有经验,所以请详细回答。

My goal is to open a modal and populate it with the first of a dozen sets of elements and use dynamically added pagination (markup and unique ids) to navigate through the sets. 我的目标是打开一个模态并用十几组元素中的第一组填充它,并使用动态添加的分页(标记和唯一ID)来浏览集合。 I can make this work if I'm not using a modal but I need a modal type of popover for this project. 如果我不使用模态,我可以使这项工作,但我需要一个模态类型的弹出窗口为这个项目。

I open the modal using a plugin that makes it easy to load ajax content: 我使用插件打开模式,可以轻松加载ajax内容:

<p><span class="smr-popup">click for popup test</span></p>

<script src="/js/eModal.min.js"></script>

<script>
$(document).ready(function () {
 $(".smr-popup").click(smrPopup);
  function smrPopup() {
    eModal.ajax({
        size: 'lg',
        url: "page1.html",
        buttons: [
            { text: "Close", close: true, style: "danger" }
        ]
    }, "Title");
   }
 });
</script>

The modal container at the bottom of the source page: 源页面底部的模态容器:

<div class="modal fade" tabindex="-1" style="display: none;" aria-hidden="true">
 <div class="modal-dialog modal-lg">
  <div class="modal-content">
   <div class="modal-header">
     <button type="button" class="x close" data-dismiss="modal">
       <span aria-hidden="true">×</span>
       <span class="sr-only">Close</span>
      </button>
      <h4 class="modal-title">Ajax modal <small></small></h4>
   </div>
  </div>
 </div>
</div>

When the modal is populated I dynamically add pagination using unique ids. 填充模态时,我使用唯一ID动态添加分页。 The problem is when I click on one of these absolutely nothing happens, the click is ignored by jquery. 问题是,当我点击其中一个绝对没有任何反应时,jquery会忽略点击。

<p>..... modal content loaded via ajax .....</p>

<span id="page2">goto page 2</span>
<span id="page3">goto page 3</span>

<script>
  $(document).ready(function() {
   $('#page2').on("click", function() { 
    $.get("page2.html");
   });
   $('#page3').on("click", function() { 
    $.get("page3.html");
   });
  });
</script>

I need to repopulate the modal with new ajax content using dynamically generated selectors and I'm going nuts trying to figure out how to get jquery to recognize the new ids. 我需要使用动态生成的选择器重新填充带有新ajax内容的模态,并且我正在努力弄清楚如何让jquery识别新的id。 Hopefully I've been clear enough that this makes sense to someone. 希望我已经足够清楚,这对某人有意义。 Thanks in advance for any help! 在此先感谢您的帮助!

The second argument to jQuery's on() function is the target selector. jQuery的on()函数的第二个参数是目标选择器。 Those click events will have to be placed on the dynamic elements parent (the p tag according to your code) and then you'll have to have the id selector argument be the second argument. 那些点击事件必须放在动态元素parent(根据你的代码的p标签)上,然后你必须将id选择器参数作为第二个参数。

when you do: 当你这样做时:

$('#page2').on("click"...

This will not find any element because it isn´t already attatch to document. 这将找不到任何元素,因为它尚未附加到文档。

eModal.ajax can get 3 arguments or 1 object, eModal.ajax可以获得3个参数或1个对象,

eModal.ajax("url",'title', function($templateAjax){
   $templateAjax.find("#page2").on('click', handler);
});

or by object 或按对象

 eModal.ajax({
          url:"url",
          title:'title', 
          callback: function($templateAjax){
            $templateAjax.find("#page2").on('click', handler);
       }
   });

Note: please make sure you are using V1.0.2 or higher of eModal.js 注意:请确保您使用的是eModal.js的 V1.0.2或更高版本

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

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