首页 > 解决方案 > 在 HTML 页面中将数据从 JSON 文件显示到引导模式

问题描述

我想使用引导模式显示来自 HTML 中的 JSON 文件的一些信息。

我有 12 个 HTML 按钮(12 个月),如果用户单击每个按钮,我想在 JSON 文件中显示数据。我的 HTML 页面是:

<!doctype html>
<html lang="en">
  <head>
    <title>show modal</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    
  </head>
  <body>
    <button type="button" class="btn btn-outline-danger btn-sm btn-block" data-toggle="modal" data-target="#modelId">Show id 1</button>
    <button type="button" class="btn btn-outline-info btn-sm btn-block" data-toggle="modal" data-target="#modelId">Show id 2</button>
    <button type="button" class="btn btn-outline-warning btn-sm btn-block" data-toggle="modal" data-target="#modelId">Show id 3</button>
    <button type="button" class="btn btn-outline-success btn-sm btn-block" data-toggle="modal" data-target="#modelId">Show id 4</button>
    <button type="button" class="btn btn-outline-danger btn-sm btn-block" data-toggle="modal" data-target="#modelId">Show id 5</button>
    <button type="button" class="btn btn-outline-info btn-sm btn-block" data-toggle="modal" data-target="#modelId">Show id 6</button>
    <button type="button" class="btn btn-outline-warning btn-sm btn-block" data-toggle="modal" data-target="#modelId">Show id 7</button>
    <button type="button" class="btn btn-outline-success btn-sm btn-block" data-toggle="modal" data-target="#modelId">Show id 8</button>
    <button type="button" class="btn btn-outline-danger btn-sm btn-block" data-toggle="modal" data-target="#modelId">Show id 9</button>
    <button type="button" class="btn btn-outline-info btn-sm btn-block" data-toggle="modal" data-target="#modelId">Show id 10</button>
    <button type="button" class="btn btn-outline-warning btn-sm btn-block" data-toggle="modal" data-target="#modelId">Show id 11</button>
    <button type="button" class="btn btn-outline-success btn-sm btn-block" data-toggle="modal" data-target="#modelId">Show id 12</button>

    <!-- Modal -->
    <div class="modal fade" id="modelId" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">MONTH NAME HERE</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                </div>
                <div class="modal-body">
                    DESCRIPTION MONTH here
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>

我的 JSON 文件数据是:

我的 JSON 文件路径是:content/js/monthjson.json

{
  "months": [{
      "id": "1",
      "georgiamonth": "Jan",
      "description": "description Jan month",
      "suggestmonths": ""
    },
    {
      "id": "2",
      "georgiamonth": "Feb",
      "description": "description Feb month",
      "suggestmonths": ""
    },
    {
      "id": "3",
      "georgiamonth": "Mar",
      "description": "description mar month",
      "suggestmonths": ""
    },
    {
      "id": "4",
      "georgiamonth": "April",
      "description": "",
      "suggestmonths": "description April month"
    },
    {
      "id": "5",
      "georgiamonth": "May",
      "description": "description May month",
      "suggestmonths": ""
    },
    {
      "id": "6",
      "georgiamonth": "June",
      "description": "description June month",
      "suggestmonths": ""
    },
    {
      "id": "7",
      "georgiamonth": "July",
      "description": "description July month",
      "suggestmonths": ""
    },
    {
      "id": "8",
      "georgiamonth": "Aug",
      "description": "description Aug month",
      "suggestmonths": ""
    },
    {
      "id": "9",
      "georgiamonth": "Sept",
      "description": "description Sept month",
      "suggestmonths": ""
    },
    {
      "id": "10",
      "georgiamonth": "Oct",
      "description": "description Oct month",
      "suggestmonths": ""
    },
    {
      "id": "11",
      "georgiamonth": "Nov",
      "description": "description Nov month",
      "suggestmonths": ""
    },
    {
      "id": "12",
      "georgiamonth": "Dec",
      "description": "description Dec month",
      "suggestmonths": ""
    }
  ]
}

谢谢您的帮助。

标签: htmljqueryjsonbootstrap-4bootstrap-modal

解决方案


您可以为每个按钮添加一个data-id属性,以便知道单击的是哪个按钮:

<button class="get-month-data ..." ... data-id="12">Show id 12</button>

然后事件处理程序代码将如下所示:

$(`button`).on(`click`, `.get-month-id`, function() {
  let id = this.attributes[`data-id`].value;

  $.getJSON(`content/js/monthjson.json`, function(months) {
    let month = months.filter(function(data) { return data.id === id; });

    $(`.modal-title`).text(month.georgiamonth);
    $(`.modal-body`).html(month.description);
  });
});

https://www.w3schools.com/tags/att_global_data.asp

https://api.jquery.com/on/

https://api.jquery.com/jquery.getjson/

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter


推荐阅读