admin 管理员组文章数量: 1086019
I'm trying to add the input values of several text boxes using javascript and display the total number below. How can I add and keep the sum for displaying after the putation. I'm not an expert in javascript.
I'm trying to add the input values of several text boxes using javascript and display the total number below. How can I add and keep the sum for displaying after the putation. I'm not an expert in javascript.
Share Improve this question edited Dec 8, 2013 at 4:52 Josh Crozier 241k56 gold badges400 silver badges313 bronze badges asked Jun 28, 2010 at 9:50 coder247coder247 2,95319 gold badges51 silver badges71 bronze badges2 Answers
Reset to default 3Here is an example that shows you how to do this:
<form name="myFormName">
<p><input type="text" name="myInputName1" value="25.3"></p>
<p><input type="text" name="myInputName2" value="14.2"></p>
</form>
<div id="total"></div>
<script type="text/javascript>
var total = parseFloat(0, 10);
total += parseFloat(document.myFormName.myInputName1.value, 10);
total += parseFloat(document.myFormName.myInputName2.value, 10);
document.getElementById("total").innerHTML = "Total is " + total;
</script>
Well, let's say you have 5 textboxes, with the id text1, text2, text3, text4 and text5:
var boxes = ['text1', 'text2', 'text3', 'text4', 'text5'],
sum = 0,
i = 0,
len = boxes.length;
for(; i < len; ++i){
sum += parseInt(document.getElementById(boxes[i]).value, 10); // Use parseFloat if you're dealing with floating point numbers.
}
alert(sum);
本文标签: adding the text box values and display it using javascriptStack Overflow
版权声明:本文标题:adding the text box values and display it using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1743992441a2514975.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论