我需要知道如何从大约10个文本字段中添加值,添加onChange的总和,并让它将总和给我一个表单中的另一个文本字段。
因此,当我点击提交时,文本字段“total”将随表单一起发送 例如:
<form>
<input name="qty" type="text" size="8" />
<input name="qty2" type="text" size="8" />
<input name="qty3" type="text" size="8" />
<input name="qty4" type="text" size="8" />
<input name="qty5" type="text" size="8" />
<input name="qty6" type="text" size="8" />
<br />
<br />
<input name="total" type="text" size="8" id="total" />
</form>
答案 0 :(得分:2)
<form id="form">
<input onkeypress="send()" onchange="send()" name="qty" type="text" size="8" />
<input onkeypress="send()" onchange="send()" name="qty2" type="text" size="8" />
<input onkeypress="send()" onchange="send()" name="qty3" type="text" size="8" />
<input onkeypress="send()" onchange="send()" name="qty4" type="text" size="8" />
<input onkeypress="send()" onchange="send()" name="qty5" type="text" size="8" />
<input onkeypress="send()" onchange="send()" name="qty6" type="text" size="8" />
<br />
<br />
<input name="total" type="text" size="8" id="total" />
</form>
<script>
function send(){
var sum=0;
var input = document.getElementById('form').getElementsByTagName('input');
for (i=0;i<input.length-1;i++){
if((input[i].name!='total')&&(!isNaN(parseInt(input[i].value)))){
sum+=parseInt(input[i].value);
}
}
document.getElementById('total').value=sum;
}
</script>