<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<script type="text/javascript">
spreadSheet= new Array(9),summary="";
for(var i=0; i<spreadSheet.length; i++)
spreadSheet[i]=0;
document.writeln("<h1>Poll<\/h1><form method='get' id='tally' action=''>");
document.writeln("<p>Enter Sales Amount:<br/><input type='text' id='cash'/><input type='button' value='calculate' onclick='calculateSalary()'/><\/p>");
document.writeln("<\/form>");
function calculateSalary(){
var value=document.getElementById("cash").value;
switch(value){
case (value<300):++spreadSheet[0]; break;
case (value<400):++spreadSheet[1]; break;
case (value<500):++spreadSheet[2]; break;
case (value<600):++spreadSheet[3]; break;
case (value<700):++spreadSheet[4]; break;
case (value<800):++spreadSheet[5]; break;
case (value<900):++spreadSheet[6]; break;
case (value<1000):++spreadSheet[7];break;
default:++spreadSheet[8];break;
}
}
function display(){
summary="Number of People who earned salaries in the following ranges: \n\n";
summary+="$200 – 299:\t" + spreadSheet[0] + "\n";
summary+="$300 – 399:\t" + spreadSheet[1] + "\n";
summary+="$400 – 499:\t" + spreadSheet[2] + "\n";
summary+="$500 – 599:\t" + spreadSheet[3] + "\n";
summary+="$600 – 699:\t" + spreadSheet[4] + "\n";
summary+="$700 – 799:\t" + spreadSheet[5] + "\n";
summary+="$800 – 899:\t" + spreadSheet[6] + "\n";
summary+="$900 – 999:\t" + spreadSheet[7] + "\n";
summary+="$1000 and over\t" + spreadSheet[8] + "\n";
return summary;
}
document.writeln("<textarea cols='80' rows='15'>"+display()+"<\/textarea>");
</script>
</head>
<body>
</body>
</html>
我是一个 javascript 新手,是的,我在发帖前确实尝试过帮助自己。
我只想制作一个简单的程序,从按钮接收用户输入,然后根据用户输入的值递增一个全局数组元素。问题是我无法让全局数组正常工作。我究竟做错了什么?