程序說明: - 我的程序使用javascript for循環將數據從名爲「arrays.js」的外部javascript數組放入HTML表格中。該程序用於在「日期名稱地址數量」表標題下放置正確的數據。Javascript/for循環/ HTML表格定位
問題: - 數組中的數據越過了界限,或者我應該說在表頭中缺少邊界設置「日期名稱地址數量」它不會在「數量」標題後創建新記錄,它只是簡單的將新記錄添加到同一行。我希望它回捲到「Date」表頭下的下一個記錄行。因此,arrya中的日期值將位於「日期」標題下,「名稱」標題下的名稱值...以下示例包含在下面: 示例:我有4個標題(日期,名稱,地址,金額)我希望在下一個數組下的下一個日期的開始被放置在「Date」表頭下。它將數組的下一個日期值放在同一行上,而不是包裝到新的記錄行。代碼如下。 日期姓名地址數
的index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Winch - Lab 10</title>
<script src="arrays.js" type="text/javascript"></script>
<script type="text/javascript">
function totalContributions()
{
var totalAmount = 0;
for (var i = 0; i < amount.length; i++)
totalAmount = totalAmount + amount[i];
return totalAmount;
}
</script>
</head>
<body>
<table id="donations">
<tr>
<th>Date</th>
<th>Name</th>
<th>Address</th>
<th>Amount</th>
</tr>
<script>
for (var x=0; x < amount.length; x++){
if (x%2==0){
document.write("<tr>");
} else {
document.write("<tr class='stripe'>");
}
document.write("<td>"+date[x]+"</td>");
document.write("<td>"+firstName[x]+" "+lastName[x]+"</td>");
document.write("<td>"+street[x]+"<br>"+city[x]+","+state[x]+" "+zip[x]);
document.write("<td class='amt'>$"+amount[x]+"</td>");
document.write("</tr)");
}
</script>
</table>
</body>
</html>
**arrays.js**
street = new Array();
city = new Array();
state= new Array();
zip = new Array();
amount = new Array();
date = new Array();
firstName[0]="Ron";
lastName[0]="Burgundy";
street[0]="88 Regal Lane";
city[0]="Williamsburg";
state[0]="KY";
zip[0]="40769";
amount[0]=625;
date[0]="2015-07-18";
firstName[1]="Ricky";
lastName[1]="Bobby";
street[1]="407 Easy Street";
city[1]="London";
state[1]="KY";
zip[1]="40744";
amount[1]=75;
date[1]="2015-07-18";
firstName[2]="Veronica";
lastName[2]="Corningstone";
street[2]="743 Stawlings Drive";
city[2]="Danville";
state[2]="KY";
zip[2]="40423";
amount[2]=50;
date[2]="2015-07-16";
firstName[3]="Brick";
lastName[3]="Tamland";
street[3]="102 Maple Lane";
city[3]="Danville";
state[3]="KY";
zip[3]="40423";
amount[3]=150;
date[3]="2015-07-15";
changed document.write(「 tr)」); to document.write(「」); **代碼現在工作,林新的stackoverflow試圖投票的答案,但我沒有enof點要這樣做?當我收到更多積分時,我會回來。謝謝「核心」我會回來獎勵你。 –
@EdwardWinch沒問題,我很高興它幫助你:)。 –