刚开始使用JS。
我有一个按钮,它调用一个函数,它应该遍历一个数组并向我显示每个项目,但它没有。它说:
未定义showWorthSum()函数。
function addWorth()
{
var table1= document.getElementById("tableNetWorths");
var rowCount1= table1.rows.length;
var row1= table1.insertRow(rowCount1);
var arr= [];
for(count = 0; count < rowCount1; count++)
{
arr.push(table1.rows[count].cells[1].innerHTML);
}
arr.shift();
return arr;
}
function showWorthSum()
{
var returnedArr= [];
returnedArr.push(addWorth());
var totalWorth= 0;
var arrCount= 10 ;
for(int count = 0; count < arrCount; count++)
{
//totalWorth= totalWorth+ returnedArr[count];
document.write(returnedArr[count]);
//debugger;
}
//return totalWorth;
}
按钮:
<button class="btn btn-primary" onclick="document.write(showWorthSum())" type="button">Show Sum</button>
是的,我已经处理了脚本标签等,只是为了发布而剥离了这些标签。
更新:(完整代码)
<!DOCTYPE html>
<html>
<head>
<link href="css/basic.css" rel="stylesheet">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script>
function alterTable()
{
//Textboxes code
var tBoxCompany= document.getElementById("txtboxCompany");
var tBoxAmount= document.getElementById("txtboxAmount");
//table code
var table= document.getElementById("tableNetWorths");
var rowCount= table.rows.length;
var row= table.insertRow(rowCount);
var Cell1= row.insertCell(0);
var Cell2= row.insertCell(1);
Cell1.innerHTML= tBoxCompany.value;
Cell2.innerHTML= tBoxAmount.value;
}
function addWorth()
{
var table1= document.getElementById("tableNetWorths");
var rowCount1= table1.rows.length;
var row1= table1.insertRow(rowCount1);
var arr= [];
for(count = 0; count < rowCount1; count++)
{
arr.push(table1.rows[count].cells[1].innerHTML);
}
arr.shift();
return arr;
}
function showWorthSum()
{
var returnedArr= [];
returnedArr.push(addWorth());
var totalWorth= 0;
var arrCount= 10 ;
for(int count = 0; count < arrCount; count++)
{
//totalWorth= totalWorth+ returnedArr[count];
document.write(returnedArr[count]);
//debugger;
}
//return totalWorth;
}
</script>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<table id="tableNetWorths">
<tr>
<th>Company</th>
<th>Net Worth ($)</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>100</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>200</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>344</td>
</tr>
<tr>
<td>Island Trading</td>
<td>22</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>122</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>99</td>
</tr>
</table>
</div>
</div>
<div class="row">
<br>
<div class="col-md-3">
<input type="text" id="txtboxCompany" class="form-control"/>
</div>
<div class="col-md-3">
<input type="text" id="txtboxAmount" class="form-control"/>
</div>
<div class="col-md-3">
<button class="btn btn-primary" onclick="alterTable()" type="button">Add Rows</button>
</div>
<div class="col-md-3">
<button class="btn btn-primary" onclick="document.write(showWorthSum())" type="button">Show Sum</button>
</div>
</div>
</div>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</body>
</html>
更新:( ShowWorthSum函数已修改,仍无效)
function showWorthSum()
{
var returnedArr= [];
returnedArr.push(addWorth());
var totalWorth= 0;
var arrCount= 10 ;
for(count = 0; count < arrCount; count++)
{
totalWorth= totalWorth+ returnedArr[count];
}
return totalWorth;
}
答案 0 :(得分:2)
问题在于您没有定义变量计数。使用此:
for(var count = 0; count < arrCount; count++){ ...
使用JS,无论你的var是字符串还是int,你总是使用var
声明你的变量
答案 1 :(得分:1)
删除&#39; int&#39;来自你的for循环定义。你有:
for(int count = 0; count < arrCount; count++)
你需要:
for(count = 0; count < arrCount; count++)
此外,您的循环将返回一系列未定义的&#39;结果,它迭代超过实际数组中的元素数量。你有六个元素,你循环固定10次。所以一旦你超过第6个元素,所有其余元素都会返回(正确)为undefined。