0
MYHTML表添加顏色到使用javascript
<table border="1" id="pay">
<thead>
<th>#</th>
<th>Admission No</th>
<th>Student Name</th>
<th>Paid Amount</th>
<th>Jan </th>
<th>Feb </th>
<th>March </th>
<th>April</th>
<th>May</th>
<th>June</th>
<th>July</th>
<th>Aug</th>
<th>Sep</th>
<th>Oct</th>
<th>Nov</th>
<th>Dec</th>
</thead>
<tbody id="payCompClass">
</tbody>
</table>
這是我的Javascript函數動態創建的表的列,
$.each(data["students"], function (i, item) {
trHTML += '<tr>' +
'<td>' + (i+1) + '</td>' +
'<td>' + item['stuid'] + '</td>' +
'<td>' + item['stu_name'] + '</td>' +
'<td>' + item['fullpayment'] + '</td>' +
'<td></td>' +
'<td></td>' +
'<td></td>' +
'<td></td>' +
'<td></td>' +
'<td></td>' +
'<td></td>' +
'<td></td>' +
'<td></td>' +
'<td></td>' +
'<td></td>' +
'<td></td>'
+'</tr>';
});
for (var y = 0; y <item['fullpayment'] ; y++) {
$('<td></td>').style.backgroundColor = "red";
}
$('#payCompClass').html(trHTML);
數據是一個數組從AJAX返回, 它持有像值對此,
|id | stuid |fullname |fullpayment |
|0 | 1010 |abcName |3 |
|1 | 1011 |abcName |2 |
|2 | 1012 |abcName |1 |
|3 | 1013 |efgName |1 |
我要的是填充顏色在TABL根據無fullpayment電子列,有沒有好辦法才達到this.Similar此,
|# | Admission |fullname|fullpayment |Jan|Feb|Mar|Apr|May|....|Dec|
|0 | 1010 |abcName |3 |red|red|red|no |no|.... |no|
|1 | 1011 |abcName |2 |red|red|no |no |no|.... |no|
|2 | 1012 |abcName |1 |red|no |no |no |no|.... |no|
|3 | 1013 |efgName |1 |red|no |no |no |no|.... |no|
香港專業教育學院嘗試了在trHtml代碼的循環結束,但沒有工作,有人建議的解決方案這個, 謝謝。
謝謝大衛@大衛JorHpan ,,這作品! – maxasela