AJAX Bootstrap模式不显示

时间:2016-01-07 15:24:43

标签: javascript php jquery ajax twitter-bootstrap

我试图显示以下模式,其中正文是数据库驱动的。但是这个数据没有出现在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 ?>">

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

3 个答案:

答案 0 :(得分:0)

尝试在<a>的点击事件上执行此操作,而不是在模态显示的事件中填充它:

$('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); 
    } 
  });  
})

答案 1 :(得分:0)

将此保存为&#34; modal_template.html&#34; :

<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(数据)应该记录您的&#34;模态模板&#34;。

编辑:

还有点修改了你的Jquery(发现有些遗漏&#34 ;;&#34;):

$('#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");
        }
    }); 
});

编辑:仅显示您的模态标头的原因是因为<?php echo $postcodeFull ?>正在完成您的AJAX请求。

答案 2 :(得分:0)

$('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); 
    } 
  });  
})