【问题标题】:Dynamic bootstrap modal within a loop循环内的动态引导模式
【发布时间】:2014-03-31 00:45:22
【问题描述】:

我在 jinja2 循环中创建跨度,当用户点击它们时,应该会出现一个带有动态内容的模式。更具体地说,动态项是每个模式内的地图图像:

<img src="http://maps.googleapis.com/maps/api/staticmap?markers=color:red%7Clabel:S%7C{{item.venue.latitude}},{{item.venue.longitude}}&zoom=17&size=400x400&sensor=false"/>

为了避免一个静态模态在循环中重复,我在模态对话框中添加了一个动态id

<div class="modal-dialog" id="{{id}}" >

但这似乎行不通。这是我的代码:

<span style="cursor: pointer;" type="button" class="badge badge-white" data-toggle="modal" data-target="#myModal">Map</span>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" id="{{id}}" >
    <div class="modal-content" >
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" id="myModalLabel">Lorem ipsum<small>Lorem ipsum</small></h4>
      </div>
      <div class="modal-body text-center pagination-centered">
        <img src="http://maps.googleapis.com/maps/api/staticmap?markers=color:red%7Clabel:S%7C{{item.venue.latitude}},{{item.venue.longitude}}&zoom=17&size=400x400&sensor=false"/>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <!--<button type="button" class="btn btn-primary">Save changes</button>-->
      </div>
    </div>
  </div>
</div>

【问题讨论】:

    标签: javascript html twitter-bootstrap jinja2


    【解决方案1】:

    如果有人仍在寻找解决方案,请尝试以下代码(注意:我已经在 PHP 中完成了它,因为您可以在代码中看到 php 块。)这肯定会起作用

    <?php
      $id=0;//define on the top of your function
        
       foreach()                     
    <button type="button" class="btn-link" data-toggle="modal" data-target="#myModal<?php echo $id;?>">Read More</button></div>
                           
                           <div id="myModal<?php echo $id;?>" class="modal fade" role="dialog">
                                <div class="modal-dialog"> <!-- Modal content-->
                                        <div class="modal-content">
                                       //Popup content
                                         </div>
    
                                </div>
                          </div>
    
    $id=$id+;//increase id value by for next iteration?>
    endforech;

    【讨论】:

      【解决方案2】:

      我想我想通了。不要将 id 保留为数字,而是在前面添加一些随机文本。例如; id="#modal{{ id }}"。 我也陷入了这个问题,这解决了它。我刚刚通过控制台使用 id='{{ id }}' 记录元素进行了检查,它在控制台上显示错误,但是当我添加一些像 id='modal{{ id }}' 这样的文本时,它工作得很好。

      【讨论】:

        【解决方案3】:

        如果你在循环中,你必须使用不同的模态ID,试试:

            <span style="cursor: pointer;" type="button" class="badge badge-white" data-toggle="modal" data-target="#myModal{{id}}">Map</span>
        <!-- Modal -->
        <div class="modal fade" id="myModal{{id}}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
          <div class="modal-dialog" id="{{id}}" >
            <div class="modal-content" >
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Lorem ipsum<small>Lorem ipsum</small></h4>
              </div>
              <div class="modal-body text-center pagination-centered">
                <img src="http://maps.googleapis.com/maps/api/staticmap?markers=color:red%7Clabel:S%7C{{item.venue.latitude}},{{item.venue.longitude}}&zoom=17&size=400x400&sensor=false"/>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <!--<button type="button" class="btn btn-primary">Save changes</button>-->
              </div>
            </div>
          </div>
        </div>
        

        【讨论】:

        • 这个我试过了,不行。我刚刚发现ids 中的一些不是唯一的。无论如何,谢谢您,我会将您的回复标记为已接受。
        • 啊伙计,还有很多其他复杂的方法,这为我解决了。非常感谢
        猜你喜欢
        • 2019-09-11
        • 2020-04-15
        • 1970-01-01
        • 1970-01-01
        • 2017-01-08
        • 2016-09-30
        • 1970-01-01
        • 2015-05-04
        • 1970-01-01
        相关资源
        最近更新 更多