当我添加一个新数字时,如何获得一些数字的即时平均值
How to get instant average of some numbers when I add a new one
我是编程新手,我已经尝试了很多方法来解决这个问题。
<form name="calc">
<input name="rezultat"/>
<input type="button" name="1" value="2" onclick="run2()"/>
<input type="button" name="1" value="3" onclick="run3()"/>
<input type="button" name="1" value="4" onclick="run4()"/>
<input type="button" name="1" value="5" onclick="run5()"/>
<input type="button" name="1" value="6" onclick="run6()"/>
<input type="button" name="1" value="7" onclick="run7()"/>
<input type="button" name="1" value="8" onclick="run8()"/>
<input type="button" name="1" value="9" onclick="run9()"/>
<input type="button" name="1" value="10" onclick="run10()"/>
</form>
这是我的HTML代码
这个想法是,当我点击特定按钮(每个按钮都有一个值和一个onclick proprety)添加一个新按钮时,立即对数字进行平均,并在"rezultat"字段中显示。
如果你能帮我写剧本,我将不胜感激。
现在,大部分解释都是在注释中内联完成的。
<form id="calc" name="calc">
<input name="rezultat" id="rezultat" />
<input type="button" name="1" value="2" />
<input type="button" name="1" value="3" />
<input type="button" name="1" value="4" />
<input type="button" name="1" value="5" />
<input type="button" name="1" value="6" />
<input type="button" name="1" value="7" />
<input type="button" name="1" value="8" />
<input type="button" name="1" value="9" />
<input type="button" name="1" value="10" />
</form>
<script type="text/javascript">
var count = 0; // global scope variable
var total = 0; // global scope variable
// define your function to calculate a new value
var calc = function(e){
// add value of button clicked to total
total += Number(e.target.value);
// add 1 to the counter
count += 1;
// set rezultat to average (total / count)
document.getElementById('rezultat').value = total / count;
// stop bubbling and default handler
return false;
}
// get array of the buttons in the form
buttons = document.querySelectorAll('#calc input[type=button]');
// loop through the buttons
for(var i=0;i<buttons.length;i++){
// bind a handler for the click event of each button
buttons[i].addEventListener('click',calc,false);
}
</script>
相关文章:
- 如何在javascript中获得与特定数字相等的随机数
- 如何限制一个字段只接受4个数字字符作为输入,并获得'It’这是潜水警报
- 当我添加一个新数字时,如何获得一些数字的即时平均值
- D3 图表过渡要么获得正确的数字或宽度,但不能同时获得两者
- Javascript提示和警报输入一个数字,它将循环,您将输入数字以获得它的平均值
- Javascript-如何从长度有时不是静态的随机字符串中获得3位数字
- 可以'当我交换表行时,我无法在JQuery函数中获得要交换的数字
- 这个最终数字从何而来
- 如何将日期减去或添加某个数字并以纪元毫秒为单位获得值
- 如何获得 [0,1] 范围内的加密强数字
- 如何根据已知值从一个数字中获得每个n
- 仅从10.0或2获得数字
- 如何在通过Ajax获取的不同页面上的HTML表中获得数字的总和
- 对2个十进制输入进行双重jQuery验证,并获得更大的数字
- 如何在谷歌地图上的每次点击事件中获得x和y数字
- 如何获得所有可能的方法,可以调用一个变量在js(包括非对象变量,如数字)
- 如何从方法中连续获得数字
- 如何在没有页面提交的情况下使用Codeigniter从输入框中获得两个数字的差值
- 列表中最频繁出现的数字(模式)-只想获得最高的值
- 如何获得所有重音字母,数字但没有“;,;;”的标签以及其他标点符号