我知道之前曾有人问过这个问题,尽管他们没有回答我的问题。我有一个Json
数组,如下所示
[
{
"Item1": "Orange",
"Item2": 40
},
{
"Item1": "Banana",
"Item2": 60
},
{
"Item1": "Lemon",
"Item2": 30
}
]
我使用Jquery
$.each(details, function (index, jsonObject) {
var k = index;
var ProductName = jsonObject.Item1;
var ProductPrice = jsonObject.Item2;
trHtml = '<tr>'
+ '<td style="padding: 5px 10px;">' + k + '</td>'
+ '<td style="padding: 5px 10px;">' + ProductName + '</td>'
+ '<td style="padding: 5px 10px;">' + ProductPrice + '</td>'
+ '</tr>'
});
但是上面的代码总是将数组的最后一行追加吗?
注意:该问题以前的所有重复内容都说要使用
var
声明您的商品名称,但这对我没有帮助!
$(function() {
var details = [];
details = [{
"Item1": "Orange",
"Item2": 40
},
{
"Item1": "Banana",
"Item2": 60
},
{
"Item1": "Lemon",
"Item2": 30
}
]
$.each(details, function(index, jsonObject) {
var k = index;
var ProductName = jsonObject.Item1;
var ProductPrice = jsonObject.Item2;
trHtml = '<tr>' +
'<td style="padding: 5px 10px;">' + k + '</td>' +
'<td style="padding: 5px 10px;">' + ProductName + '</td>' +
'<td style="padding: 5px 10px;">' + ProductPrice + '</td>' +
'</tr>'
});
$("#classTable tbody").append(trHtml);
$('#classModal').css('display', 'block');
$('#classModal').modal('show');
})
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" type="text/css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js" type="text/javascript"></script>
<div id="classModal" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="classInfo" aria-hidden="true" style="display:none;">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
×
</button>
<h4 class="modal-title" id="classModalLabel">
Purchase Info
</h4>
</div>
<div class="modal-body">
<table id="classTable" class="table table-bordered">
<thead>
<tr>
<th>NO.</th>
<th>Product</th>
<th>Price</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">
Close
</button>
</div>
</div>
</div>
</div>