希望这是我的最后一个问题,但这里是。
我有一个HTML脚本:
<input type="text" name="copy1" id="copy1" size="4" maxlength="10" onchange="calc1(this.value)">
<input type="text" name="copycosts" id="copycosts" size="4" VALUE="">
<input name="copy2" type="text" id="copy2" size="4" maxlength="10" onchange="calc2(this.value)">
等....我想将它们全部加在一起,以便它们总计在总框中。我对每个人都有不同的功能,这样他们都可以获得成本并且效果很好。我无法得到这笔钱。 我试过javascripting它:
function sum(){
var cc = document.getElementById("copycosts").value;
var cc2 = document.getElementById("copycosts2").value;
var cc3 = document.getElementById("copycosts3").value;
var sc = document.getElementById("searchcosts").value;
var cc4 = document.getElementById("certcosts").value;
var nc = document.getElementById("naracosts").value;
var ac = document.getElementById("apostcosts").value;
var sum = parseFloat(cc + cc2 + cc3 + sc + cc4 + nc + ac);
document.getElementById("sum").value = sum;
}
我做错了什么?
再次感谢
答案 0 :(得分:0)
HTML:
<input type="text" class="numInput" id="copy1" maxlength="10" />
<input type="text" class="numInput" id="copy2" maxlength="10" />
<input type="text" class="numInput" id="search" maxlength="10" />
<input type="text" class="numInput" id="nara" maxlength="10" />
Sum:
<input type="text" class="numOutput" id="sum" readonly="true" />
JavaScript的:
var load = function () {
'use strict';
var inElems = document.getElementsByClassName('numInput'),
sum = document.getElementById('sum'),
i;
var add = function () {
var i, result = 0;
for (i = 0; i < inElems.length; i++) {
result += +(inElems.item(i).value);
}
sum.value = result;
};
for (i = 0; i < inElems.length; i++) {
inElems.item(i).addEventListener('input', add, false);
}
}
(function () {
document.addEventListener('load', load, false);
})();
CSS:
.numInput {
width: 60px;
}
.numOutput {
width: 90px;
readonly: true;
}
注意:此示例使用可能与旧版浏览器不兼容的现代JavaScript技术