简体   繁体   English

将JSON数据加载到Bootstrap模式中

[英]Load JSON data into a Bootstrap modal

I want to load a JSON file that creates a list inside a Bootstrap Modal. 我想加载一个JSON文件,在Bootstrap模式中创建一个列表。 I have it set where if you click on a person's picture, the modal pops up. 我把它设置在哪里如果你点击一个人的图片,模式会弹出。

<li class="project span3" data-type="pfa">
    <a data-toggle="modal" data-target="#myModal" class="thumbnail">
        <img src="img/anon.jpg" alt="Kenneth Atkins" />
        <h1>Kenneth Atkins</h1>
        <p>[Description here]</p>
    </a>     
</li>

Here's an example of the JSON data: 以下是JSON数据的示例:

    var florida_exoneration = [
  {
    "last_name":"Atkins",
    "first_name":"Kenneth",
    "age":16,
    "race":"Caucasian",
    "state":"FL",
    "crime":"Sexual Assault",
    "sentence":"10 years",
    "conviction":2004,
    "exonerated":2008,
    "dna":"",
    "mistaken witness identification":"",
    "false confession":"",
    "perjury/false accusation":"Y",
    "false evidence":"",
    "official misconduct":"",
    "inadequate legal defense":"",
    "compensation":""
  }  
]

I'd like the modal to display something like this inside the box: 我希望模式在框内显示如下内容:

Title = "first_name + last_name"
Age = "age"
Race = "race"
State = "state"
""
""

I also want to make sure the data is tied to the picture so the modal doesn't get confused. 我还想确保数据与图片相关联,这样模态就不会混淆了。 I'm sorry if this is a bit confusing. 如果这有点令人困惑,我很抱歉。 I'll try and clarify if anyone has any questions. 如果有人有任何问题,我会尽力澄清。

Method 1: using Ajax 方法1:使用Ajax

Every time a user clicks an image, you get the id from the clicked image and then you send an Ajax request to server in order to get the JSON object. 每次用户单击图像时,您都会从单击的图像中获取ID,然后向服务器发送Ajax请求以获取JSON对象。

HTML HTML

<ul>
    <li class="project span3" data-type="pfa">
    <a href="#" data-id="2" class="thumbnail">
        <img src="img/anon.jpg" alt="Kenneth Atkins" />
        <h1>Kenneth Atkins</h1>
        <p>[Description here]</p>
    </a>     
    </li>
</ul>

JavaScript JavaScript的

(function($) {
    var infoModal = $('#myModal');
    $('.thumbnail').on('click', function(){
        $.ajax({ 
          type: "GET", 
          url: 'getJson.php?id='+$(this).data('id'),
          dataType: 'json',
          success: function(data){ 
            htmlData = '<ul><li>title: '+data.first_name+'</li><li>age: '+data.age+'</li></ul>';
            infoModal.find('.modal-body').html(htmlData);
            infoModal.modal('show');
          }
        });

        return false;
    });
})(jQuery);

Method 2: using hidden div 方法2:使用隐藏的div

No need to any Ajax request, but you need to create a hidden div that contain all the information you want to display in the modal 不需要任何Ajax请求,但您需要创建一个隐藏的div,其中包含您要在模式中显示的所有信息

HTML HTML

<ul>
    <li class="project span3" data-type="pfa">
    <a href="#" class="thumbnail">
        <img src="img/anon.jpg" alt="Kenneth Atkins" />
        <h1>Kenneth Atkins</h1>
        <p>[Description here]</p>
        <div class="profile hide">
            <ul>
                <li>title: Atkins Kenneth</li>
                <li>Age: 16</li>
            </ul>
        </div>
    </a>     
    </li>
</ul>

JavaScript JavaScript的

(function($) {
    var infoModal = $('#myModal');
    $('.thumbnail').on('click', function(){
        htmlData = $(this).find('.profile').html();
        infoModal.find('.modal-body').html(htmlData);
        infoModal.modal('show');
        return false;
    });
})(jQuery);

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

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