Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript获得两个不同函数的结果并将它们添加到一起_Javascript_Html - Fatal编程技术网

Javascript获得两个不同函数的结果并将它们添加到一起

Javascript获得两个不同函数的结果并将它们添加到一起,javascript,html,Javascript,Html,我得到了两个不同的javascript函数sum和diff,它们有两个结果。问题是,如何获得2函数的结果,并将其添加到另一个文本框中显示 function sum() { var basicpay = document.getElementById('basicpay').value; var overtime = document.getElementById('overtime').value; var regularholiday = document

我得到了两个不同的javascript函数sum和diff,它们有两个结果。问题是,如何获得2函数的结果,并将其添加到另一个文本框中显示

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 others1 = document.getElementById('others1').value;
      var grosspay = document.getElementById('grosspay').value;
      var monthpay13 = document.getElementById('monthpay13').value;

        var result = 

        parseInt(basicpay) + 
        parseInt(overtime) +
        parseInt(regularholiday) +
        parseInt(specialholiday) +
        parseInt(allowanceday) +
        parseInt(others1) +
        parseInt(grosspay) +
        parseInt(monthpay13);


        if (!isNaN(result)) {
            document.getElementById('totalincome').value = result;
        }
        }
另一个函数在这里:

function diff() {           

        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(absent) - 
        parseInt(tardiness) -
        parseInt(sss) -
        parseInt(pagibig) -
        parseInt(philhealth) -
        parseInt(cashadvances) -
        parseInt(withholdingtax) -
        parseInt(others);


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

        }
        }

我只想将总扣减额和总收入分包,并显示在另一个文本框中。提前感谢

将此函数添加到Javascript的顶部:

function calculate() {
   document.getElementById('both').value = sum() + diff();
}
添加新的HTML元素以保存您的答案:

19.<input type="text" Placeholder="Both" id="both" />
最后,您需要更改“var result=…”代码,否则它只在输入了所有数字后才起作用


我建议您减少手工操作,让文本框自动绑定到变量。完成其中一些教程,您将看到它是多么简单:

无需修改函数,您可以这样做:

window.onload = function() {
    document.getElementById('othertextbox').value = parseInt(document.getElementById('totalincome').value) - parseInt(document.getElementById('totaldeductions').value);
}

(将上述代码添加到您的Javascript中)

我应该把它放在哪里,如何显示它,先生?让它工作起来,请参阅我更改的答案。如果这有帮助,请标记为答案。先生,您的意思是什么?您需要更改“var result=…”代码,我应该在那里更改什么?
window.onload = function() {
    document.getElementById('othertextbox').value = parseInt(document.getElementById('totalincome').value) - parseInt(document.getElementById('totaldeductions').value);
}