另外,使用Javascript功能添加多个文本框

Addition, Add Multiple textbox using Javascript function

本文关键字:文本 添加 功能 使用 Javascript 另外      更新时间:2023-09-26

我有这个问题,我添加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;
        }
        }

问题是您对字段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);

我认为这个问题是因为您的616数字文本框具有相同的ID。

更改文本框编号16的ID,它应该工作

问题发生在最后一个字段。这是由于您有两个具有相同ID的框。要解决这个问题,请更改最后一个框ID from others to others2。同时将变量others改为others2因为有两个变量others