我有一个脚本需要捕获一个值(textbox1),并使用该值来确定数组的大小。然后我需要使用不同的文本框(testbox2)来捕获值以填充数组,然后处理一些计算。
如果我使用窗口提示进行数据捕获,我可以很好地捕获第一个值,调整数组大小并填充数组。我不知道如何用文本框捕获替换提示方法。
这是代码 - 评论显示了我卡在哪里。本质上,我需要将“window.prompt”部分替换为“按 id 获取元素”并使用文本框,循环遍历这些值,直到输入的初始数字(文本框 1)被满足。
<html>
<head>
<meta charset = "utf-8">
<title>Variance and Standard Deviation</title>
<style type = "text/css">
#placement {
width: 50%;
padding: 0 0px;
align:right;
text-align: left;
}
#sorted {
width: 50%;
padding: 0 0px;
float:left;
text-align: left;
}
#mean,{
width: 50%;
padding: 0 0px;
align:right;
text-align: left;
}
#variance,{
width: 50%;
padding: 0 0px;
align:right;
text-align: left;
}
#deviation,{
width: 50%;
padding: 0 0px;
position: relative;
text-align: left;
}
</style>
<script>
//declare and intialize variables
var capCount = 0; //capture count of variables to be entered
var count = 0; //parse captured count to an integer
var arrayTotal=0 ; //total of all values entered into array for math calcs
var arrayAverage=0; //average (mean) of values in array
var sortedArray = 0; //array sorted in ascending order
var values = 0; //values entered into text box for array population
function start(formInfo)
{
var countField = document.getElementById( "count" ); // gets entry from count field on form
var capCount = countField.value;
var count = parseInt (capCount);
var valueArray = new Array(); // allocate empty array
//start capture and populate array loop
for ( var i = 0; i < count; ++i ) //using loops count, prompts user to enter variables until count of loops (count) is reached
//STUCK HERE- replace variable capture from window prompt to get value from text box
// rather than using window prompt box, need to capture input from the "value" text box,
// loop through the text box each time a value is entered and process when the last value (loops equal to count) is entered
{
var variable = window.prompt("Please enter a value for value " + (i + 1) + "" ); //prompts user to enter a value
//var variable = document.getElementById("value");
valueArray [ i ] = variable ;
} //end capture for loop
//calls sort function to sort array into ascending order
valueArray.sort ( sort );
//calls calcuate function to conduct math and get results
calculate ( valueArray );
}// end build array function
//sort function
function sort ( value1, value2 )
{
return parseInt ( value1 ) - parseInt( value2 );
} //end sort
//calcuate function
function calculate (theArray)
{
//declare variables
n = 0;
arrayTotal = 0;
total_sqr = 0;
for (var i = 0; i < theArray.length; ++i )
{
arrayTotal += +theArray[i];
n = (n + 1);
total_sqr += (+theArray[i] * +theArray[i]);
}
//mean, variance, deviation calcs
mean = (arrayTotal / theArray.length); //calcuate mean
variance = (total_sqr - ((arrayTotal*arrayTotal)/n))/(n - 1);
deviation = Math.sqrt( variance) ;
//calls output function to output the resuls of the script
output( "The sorted order of the values entered is", theArray, document.getElementById( "sorted" ) );
output( "The mean of the values entered is", mean.toFixed(2), document.getElementById( "mean" ) );
output( "The variance of the values entered is", variance.toFixed(2), document.getElementById( "variance" ) );
output( "The standard deviation of the values entered is", deviation.toFixed(2), document.getElementById( "deviation" ) );
} //end calculations
//output function
function output ( heading, data, results )
{
var content = "<p>" + heading + "</p><p>" + data + "</p>" ;
results.innerHTML = content; // place the table in the output element
} //end output
</script>
</head>
<body>
<body>
<P><h2>This Page calcuates mean, variance and standard deviation</h2></p>
<form action = "#">
<p><label><h3>Enter the # of values to process and click submit:</h3>
<input id = "count" type = "number" size = "10"></label>
<div>
<input type="button" value="Submit #of values to " onClick="start(this.form);">
</div>
<p><label><h3>Enter the x value to process:</h3>
<input id = "value" type = "number" size = "10"></label>
<p>
<input type="button" value="process your values" onClick="process(this.form);">
</p>
</form>
<div id = "sorted"></div>
<div id = "placement">
<div id = "mean"></div>
<div id = "variance"></div>
<div id = "deviation"></div>
</div>
</body>
</html>