【发布时间】:2021-03-13 19:39:26
【问题描述】:
我想使用引导模式显示来自 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">×</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": ""
}
]
}
感谢您的帮助。
【问题讨论】:
-
试试这个答案stackoverflow.com/questions/8787596/rendering-json-using-jquery 你可以使用点符号来访问你需要的值。所以把整个对象放在一个变量中
-
可以使用ajax访问json文件吗?还是在代码中静态可用?
-
是的 json 文件可以在文件夹中访问
标签: html jquery json bootstrap-4 bootstrap-modal