让我建立了一个简单表单的客户端进行基本计算。有6个下降都包含N / A,1,2,3。最后,从这些下降计算总体平均值。客户端想要完全忽略包含N / A的任何下降,这意味着如果N / A在6个下降中的2个中,那么平均值应该仅在4个下降时计算。如果用户选择了N / A,我只是迷失了逻辑或if语句需要告诉总字段忽略某个下拉字段。 这是我的代码,不起作用。请提出一些想法。
<html>
<head><script type="text/javascript">
var aFieldNames = new Array();
aFieldNames["N/A"]=0;
aFieldNames["1"]=1;
aFieldNames["2"]=2;
aFieldNames["3"]=3;
var nCount = 0;
var nSum = 0;
event.value = 0;
for(i = 0; i < aFieldNames.length; i++)
{
if(this.getField(aFieldNames[i]).valueAsString != "N/A")
{
nCount++;
nSum += parseFloat(this.getField(aFieldNames[i]).value);
}
}
if(nCount != 0)
{
event.getElementById("bpover").value = nSum / nCount;
}
</script>
</head>
<body>
<select name="Value[]" onChange="alert(this.value):">
<option name="N/A">N/A</option>
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
</select><br>
<select name="Value[]" onChange="alert(this.value):">
<option name="N/A">N/A</option>
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
</select><br>
<select name="Value[]" onChange="alert(this.value):">
<option name="N/A">N/A</option>
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
</select><br>
<select name="Value[]" onChange="alert(this.value):">
<option name="N/A">N/A</option>
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
</select><br>
<select name="Value[]" onChange="alert(this.value):">
<option name="N/A">N/A</option>
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
</select><br>
<br>
avg <input type="text" name="Average" ID="bpover" readonly>
</body>
</html>
答案 0 :(得分:3)
<强>首先强>
在你的陈述中:
<select name="Value[]" onChange="alert(this.value):">
您正在使用无用的:而是使用; 。
<强>第二强>
parseFloat()
或parseInt()
将获得的值转换为数字来计算平均值。请参阅以下代码:
<script>
function calculateAverage()
{
var boxes = document.getElementsByTagName('select')
var nSum = 0;
var nCount = 0;
event.value = 0;
for(var i=0; i<boxes.length;i++)
{
if(boxes[i].value != "N/A")
{
alert(boxes[i].value);
nSum += parseFloat(boxes[i].value);
nCount++;
}
}
var avg = nSum/nCount;
document.getElementById('bpover').value = avg;
}
</script>
<select name="Value[]" onChange="alert(this.value);">
<option name="N/A">N/A</option>
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
</select><br>
<select name="Value[]" onChange="alert(this.value);">
<option name="N/A">N/A</option>
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
</select><br>
<select name="Value[]" onChange="alert(this.value);">
<option name="N/A">N/A</option>
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
</select><br>
<select name="Value[]" onChange="alert(this.value);">
<option name="N/A">N/A</option>
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
</select><br>
<select name="Value[]" onChange="alert(this.value);">
<option name="N/A">N/A</option>
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
</select><br>
<br>
<button onClick="calculateAverage()">
Calculate
</button>
avg <input type="text" name="Average" ID="bpover" readonly>
答案 1 :(得分:1)
你走了! @Shakti Phartiyal的答案解释了错误的事情。
function calcAvg(){
//Get all elements with 'class="select"'
var selects = document.getElementsByClassName("select");
//Initialize vars
var avg = 0;
var count = 0;
//Calculate average
for(var i=0;i<selects.length;i++){
if(selects[i].value!="N/A"){
count++;
avg+=Number(selects[i].value);
//Alert for debugging purposes
//alert(selects[i].value+" "+avg);
}
}
avg=avg/count;
//Output average
document.getElementById("bpover").value=avg;
}
<select class="select" name="Value[]" onChange="calcAvg();">
<option name="N/A">N/A</option>
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
</select><br>
<select class="select" name="Value[]" onChange="calcAvg();">
<option name="N/A">N/A</option>
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
</select><br>
<select class="select" name="Value[]" onChange="calcAvg();">
<option name="N/A">N/A</option>
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
</select><br>
<select class="select" name="Value[]" onChange="calcAvg();">
<option name="N/A">N/A</option>
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
</select><br>
<select class="select" name="Value[]" onChange="calcAvg();">
<option name="N/A">N/A</option>
<option name="1">1</option>
<option name="2">2</option>
<option name="3">3</option>
</select><br>
<br>
avg <input type="text" name="Average" id="bpover" readonly>