只是想问问。我想显示从 JSON 链接检索的数据列表。一切都很好,直到我在每行数据中添加一个查看按钮。此按钮将显示所选数据的详细信息。现在,我想添加 jscookie 以将基于所选行的 id 带到 viewDetails.html。但我不确定代码是否正确。你们可以检查一下吗?
var badgeid = '10010079';
$.getJSON('https://huhu.haha.com/hihi/huhu.asmx/ot_displayTask?badgeid=' + badgeid + '&reportStatus=&status=yes', function(data) {
$.each(data.otReportList, function(key, data) {
// console.log(key, data);
let current_datetime = new Date(data["report_date"])
let formatted_date = current_datetime.getDate() + "-" + (current_datetime.getMonth() + 1) + "-" + current_datetime.getFullYear()
var text = `<tr>
<td>${data["report_id"]}</td>
<td>${data["task_name"]}</td>
<td>${formatted_date}</td>
<td>
<form method="POST" action="viewDetails.html">
setCookie('reportid', data["report_id"], 0.125),
setCookie("viewid", "yes", 0.125);
<button type="submit" name="reportid">View</button>
</form>
</td>
</tr>`
$("#mypanel").append(text);
})
});
table,
td,
th {
border: 1px solid black;
}
table {
width: 100%;
border-collapse: collapse;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript - read JSON from URL</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="jscookie.js"></script>
</head>
<body>
<div>
<table id="mypanel">
<tr>
<th>Report ID</th>
<th>Task Name</th>
<th>Report</th>
<th>Action</th>
</tr>
</table>
</div>
</body>
</html>