另外,使用Javascript函数添加多个文本框

时间:2014-07-19 09:18:42

标签: javascript html function

我有这个问题,我添加了8个文本框,它工作正常,但当我添加更多的文本框,如16文本框,它不会添加最后一个文本框。有没有人遇到过这个问题?提前致谢。

Live Link: JAVASCRIPT.

function sum() {

        var basicpay = document.getElementById('basicpay').value;
        var overtime = document.getElementById('overtime').value;
        var regularholiday = document.getElementById('regularholiday').value;
        var specialholiday = document.getElementById('specialholiday').value;
        var allowanceday = document.getElementById('allowanceday').value;
        var others = document.getElementById('others').value;
        var grosspay = document.getElementById('grosspay').value;
        var monthpay13 = document.getElementById('monthpay13').value;
        var absent = document.getElementById('absent').value;
        var tardiness = document.getElementById('tardiness').value;
        var sss = document.getElementById('sss').value;
        var pagibig = document.getElementById('pagibig').value;
        var philhealth = document.getElementById('philhealth').value;
        var cashadvances = document.getElementById('cashadvances').value;
        var withholdingtax = document.getElementById('withholdingtax').value;
        var others = document.getElementById('others').value;



        var result = 

        parseInt(basicpay) + 
        parseInt(overtime) +
        parseInt(regularholiday) +
        parseInt(specialholiday) +
        parseInt(allowanceday) +
        parseInt(others) +
        parseInt(grosspay) +
        parseInt(absent) + 
        parseInt(tardiness) +
        parseInt(sss) +
        parseInt(pagibig) +
        parseInt(philhealth) +
        parseInt(cashadvances) +
        parseInt(withholdingtax) +
        parseInt(others) +
        parseInt(monthpay13);

        if (!isNaN(result)) {
            document.getElementById('totalincome').value = result;
        }
        }

3 个答案:

答案 0 :(得分:1)

问题是您对字段6和字段16使用相同的ID others。为它们提供不同的ID

16.<input type="text" id="others1" Placeholder="More others"  onkeyup="sum();" /><br>

改变JS:

var others1 = var others = document.getElementById('others1').value;

var result = 

parseInt(basicpay) + 
parseInt(overtime) +
parseInt(regularholiday) +
parseInt(specialholiday) +
parseInt(allowanceday) +
parseInt(others) +
parseInt(grosspay) +
parseInt(absent) + 
parseInt(tardiness) +
parseInt(sss) +
parseInt(pagibig) +
parseInt(philhealth) +
parseInt(cashadvances) +
parseInt(withholdingtax) +
parseInt(others1) +
parseInt(monthpay13);

答案 1 :(得分:0)

我认为该问题是因为您的616号码文本框具有相同的ID。

更改文本框号16的ID,它应该有效。

答案 2 :(得分:0)

问题发生在最后一个字段上。这是因为您有两个具有相同ID的框。要修改该更改,请输入最后一个框ID from others to others2。并且还将名为others的变量更改为其他2,因为有两个变量称为others