【问题标题】:AJAX Bootstrap Modal Not ShowingAJAX Bootstrap 模式不显示
【发布时间】:2016-04-12 01:30:55
【问题描述】:

我正在尝试显示以下模式,其中正文是数据库驱动的。但是,此数据并未出现在 Bootstrap 模式中。我已经看这个太久了,看不到问题!

模态

<div class="modal fade" id="eventDetailsModal" tabindex="-1" role="dialog" aria-labelledby="memberModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="memberModalLabel">Event Details</h4>
            </div>
            <div class="dash">

            </div>

        </div>
    </div>
</div>

AJAX

$('#eventDetailsModal').on('show.bs.modal', function (event) {
      var button = $(event.relatedTarget) // Button that triggered the modal
      var recipient = button.data('whatever') // Extract info from data-* attributes
      var modal = $(this);
      var dataString = recipient;

        $.ajax({ 
            type: "GET", 
            url: "/getNearestPC.php", 
            data: {"foo": dataString}, 
            cache: false, 
            success: function (data) { 
                console.log(data); 
                console.log(dataString); 
                console.log(recipient); 
                $('.dash').html(data); 
            }, 
            error: function(err) { 
                console.log(err); 
            } 
        });  
})

getNearest.php

    //SQL Setup
    $PC = $_GET['foo']; 

<div class="modal-body center" style="background-color: #FFFFFF">
    <table id="preusertable" class="table table-striped table-hover table-curved" style="width: 100%;">
        <thead>
            <tr>
                <th style="color: #000000;">Event</th>
                <th style="color: #000000;">Postcode</th>
            </tr>
        </thead>
        <tbody> 
        <?php
            while ($row = $sData->fetch(PDO::FETCH_ASSOC)) {
                $school = $row['SchoolURL'];
                $postcodeFull = $row['Postcode'];
                ?>
                <tr>                    
                    <td style="font-size: 1.1em;"><?php echo $school ?></td>
                    <td style="font-size: 1.1em;"><?php echo $postcodeFull ?></td>
                </tr>
                <?php
            }
        ?>
        </tbody>
    </table>
</div>

模态触发器

<a data-toggle="modal" data-target="#eventDetailsModal" data-whatever="<?php echo $postcode ?>">

任何帮助都会很棒!它只显示模态标题但没有正文

【问题讨论】:

  • 注意:控制台没有错误
  • 你的 console.log(data); 是什么AJAX 调用后的输出?应该没什么吧?
  • @Y.Hermes 确实,什么都没有
  • 您的问题是您的“php 代码”。我会快速为您重写并发布它作为可能的答案。

标签: javascript php jquery ajax twitter-bootstrap


【解决方案1】:

不要在模式显示事件上填充它,而是尝试在&lt;a&gt; 的点击事件上执行此操作:

$('a').on('click', function (event) {
  var button = $(this); // Button that triggered the modal
  var recipient = button.data('whatever'); // Extract info from data-* attributes
  var modal = $("#eventDetailsModal");
  var dataString = recipient;

  $.ajax({ 
    type: "GET", 
    url: "/getNearestPC.php", 
    data: {"foo": dataString}, 
    cache: false, 
    success: function (data) { 
      console.log(data); 
      console.log(dataString); 
      console.log(recipient); 
      $('.dash').html(data); 
    }, 
    error: function(err) { 
      console.log(err); 
    } 
  });  
})

【讨论】:

  • 可悲的是做同样的事情,只是显示模态标题而不是作为 AJAX 部分的正文。不过谢谢!
  • @Frog82 等等,你说控制台没有错误。您的网络选项卡显示 PHP 的返回值是什么?
  • @Frog82 与您查看控制台的方式相同。应该还有另一个东西叫做网络。在单击按钮之前,请清除所有内容。然后单击按钮查看任何被触发的 AJAX 调用。
  • 据我所见,没有任何东西通过网络
  • @Frog82 啊哈...那时 AJAX 没有触发。事件是否已附加?点击试试console.log()
【解决方案2】:

将此保存为您的“modal_template.html”:

<div class="modal-body center" style="background-color: #FFFFFF">
<table id="preusertable" class="table table-striped table-hover table-curved" style="width: 100%;">
    <thead>
        <tr>
            <th style="color: #000000;">Event</th>
            <th style="color: #000000;">Postcode</th>
        </tr>
    </thead>
    <tbody> 
        {TBODY}
    </tbody>
</table>

这应该是您的 AJAX 请求:

        <?php
        $PC = $_GET['foo']; 
        $tmpl = file_get_contents("modal_template.html");
        $replace = "";
        while ($row = $sData->fetch(PDO::FETCH_ASSOC)) {
            $school = $row['SchoolURL'];
            $postcodeFull = $row['Postcode'];
            $replace .=  '<tr><td style="font-size: 1.1em;">' . $school . '</td><td style="font-size: 1.1em;">' . $postcodeFull . '</td></tr>';
        }
        $tmpl = str_replace("{TBODY}", $replace, $tmpl);
        echo $tmpl;
    ?>

这样,您将获得一个清晰的模态模板。如果您已经发布了所有代码,这应该会对您有所帮助,并且您的 console.log(data) 应该记录您的“模态模板”。

编辑:

还稍微修改了您的 Jquery(发现一些缺失的“;”):

$('#YOUR MODAL TRIGGER ID').on('click', function (event) {
event.preventDefault;
var button = $(event.relatedTarget) // Button that triggered the modal
var recipient = button.data('whatever') // Extract info from data-* attributes
var modal = $(this);
var dataString = recipient;

$.ajax({ 
    type: "GET", 
    url: "/getNearestPC.php", 
    data: {"foo": dataString}, 
    cache: false
    }).done(function(data){
        if(data != ""){
            console.log(data); 
            console.log(dataString); 
            console.log(recipient); 
            $('.dash').html(data);
            $('#eventDetailsModal').modal("toggle");
        }
        else{
        console.log("error");
        }
    }); 
});

编辑:只显示您的模式标题的原因是因为&lt;?php echo $postcodeFull ?&gt; 正在完成您的 AJAX 请求。

【讨论】:

    【解决方案3】:

    $('a').on('click', function (event) {
      var button = $(this); // Button that triggered the modal
      var recipient = button.data('whatever'); // Extract info from data-* attributes
      var modal = $("#eventDetailsModal");
      var dataString = recipient;
    
      $.ajax({ 
        type: "GET", 
        url: "/getNearestPC.php", 
        data: {"foo": dataString}, 
        cache: false, 
        success: function (data) { 
          console.log(data); 
          console.log(dataString); 
          console.log(recipient); 
          $('.dash').html(data); 
        }, 
        error: function(err) { 
          console.log(err); 
        } 
      });  
    })

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-07
      • 2022-12-21
      • 2023-01-17
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 2017-09-20
      • 2018-08-04
      相关资源
      最近更新 更多