将txtfields的值添加到另一个txtfield

Add value of txtfields to another txtfield

本文关键字:另一个 txtfield 添加 txtfields      更新时间:2023-09-26

我需要知道如何从大约10个文本字段中添加值,在Change上添加总和,并让它将总和输入表单中的另一个文本字段。

因此,当我点击提交时,文本字段"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>
<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>