JavaScript:使用一个文本框值作为循环的基础来捕获第二个文本框中的值?
我有一个脚本需要捕获一个值(textbox1),并使用该值来建立数组的大小。然后,我需要使用不同的文本框(tesxtbox2)来捕获值以填充数组,然后处理一些计算 如果使用窗口提示进行数据捕获,我可以捕获第一个值、调整数组大小并填充数组。我不知道如何用文本框捕获替换prompt方法 下面是代码,注释显示了我的困境。本质上,我需要将“window.prompt”部分替换为“getelementbyid”,并使用文本框,循环遍历这些值,直到完成输入的初始数字(文本框1)JavaScript:使用一个文本框值作为循环的基础来捕获第二个文本框中的值?,javascript,Javascript,我有一个脚本需要捕获一个值(textbox1),并使用该值来建立数组的大小。然后,我需要使用不同的文本框(tesxtbox2)来捕获值以填充数组,然后处理一些计算 如果使用窗口提示进行数据捕获,我可以捕获第一个值、调整数组大小并填充数组。我不知道如何用文本框捕获替换prompt方法 下面是代码,注释显示了我的困境。本质上,我需要将“window.prompt”部分替换为“getelementbyid”,并使用文本框,循环遍历这些值,直到完成输入的初始数字(文本框1) &l
<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>
方差和标准差
#安置{
宽度:50%;
填充:0 0px;
对齐:右;
文本对齐:左对齐;
}
#分类{
宽度:50%;
填充:0 0px;
浮动:左;
文本对齐:左对齐;
}
#意思是{
宽度:50%;
填充:0 0px;
对齐:右;
文本对齐:左对齐;
}
#差异{
宽度:50%;
填充:0 0px;
对齐:右;
文本对齐:左对齐;
}
#偏差{
宽度:50%;
填充:0 0px;
位置:相对位置;
文本对齐:左对齐;
}
//声明并初始化变量
var-capCount=0//捕获要输入的变量计数
var计数=0//将捕获的计数解析为整数
var-arrayTotal=0//为数学计算输入到数组中的所有值的总和
var arrayAverage=0//数组中值的平均值
var-Darray=0//按升序排序的数组
var值=0//为数组填充在文本框中输入的值
功能启动(formInfo)
{
var countField=document.getElementById(“count”);//从表单上的count字段获取条目
var capCount=countField.value;
var count=parseInt(capCount);
var valueArray=new Array();//分配空数组
//开始捕获并填充数组循环
对于(var i=0;i“+数据+””;
results.innerHTML=content;//将表放在输出元素中
}//结束输出
本页计算平均值、方差和标准差
输入要处理的值的#,然后单击提交:
输入要处理的x值:
//pseudo code
function process(){
var length = document.getElementById('count').value
var value_string = get_the_value_from_second_text_box
convert value_string to an array(of values you need)
do_the_computation
display_the_results