Javascript在两个不同的函数中获得结果并将它们添加在一起
Javascript getting result in 2 different functions and Adding them together
我得到了这2个不同的javascript函数sum和diff,他们有2个结果。问题是,我怎么能得到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" />
将所有onkeyup更改为:
onkeyup="calculate();"
最后,你需要改变你的"var result =…"代码,否则它只在输入所有数字时才有效。
我建议你少做手工工作,让你的文本框自动绑定到变量。做一些这样的教程,你会发现它是多么容易:http://learn.knockoutjs.com/#/?tutorial=intro
在不修改函数的情况下,可以这样做:
window.onload = function() {
document.getElementById('othertextbox').value = parseInt(document.getElementById('totalincome').value) - parseInt(document.getElementById('totaldeductions').value);
}
(将上述代码添加到Javascript中)
相关文章:
- 使用php脚本验证访问者的年龄,并在不刷新的情况下根据结果加载iframe
- 避免将lib依赖项与webpack+handlebas加载程序捆绑在一起
- 把两个数字加在一起,得到5+10=510,而不是15
- 将图像组合在一起并在窗口加载时对图像进行动画处理
- 在 jQuery 中将字符串加在一起
- 如何避免使用OnChange和Marketo将表单加载在一起
- 用javascript将2个int变量加在一起
- 将两个字符串加在一起是一个变量
- 如何将结果加在$中.每一个循环
- 选中复选框时填充的变量,然后将它们加在一起-获取NaN
- Javascript在两个不同的函数中获得结果并将它们添加在一起
- 试着把这些东西组合在一起——JavaScript、HTML、PHP和重新加载内容
- Angular JS将两个输入的字符长度加在一起
- 我如何将两个日期/时间加在一起?
- 把数字加在一起
- 如何将随机数添加到结果处的存储变量中,作为一个数字,而不是两个字符串加在一起
- 将ng个模型加在一起angularjs
- 当 URL 与 www 在一起时,XMLHttpRequest 无法加载
- Javascript变量不是全局返回/加在一起
- 将forEach循环中的每个数组加在一起