0

我有 2 个文本字段:“收入”“扣除”,收入有一些组成部分,如HRA、基本工资等,扣除也有一些..

我还有另外 2 个文本字段“总收入”“总扣除额”

Total Earnings = earning component1 + earning component2

Total Deductions = deduction component1 + deduction component2

我想要的是我需要编写两个函数来显示总收入和总扣除的结果。我的意思是如果我输入“收入”和“扣除”的值,这些应该自动反映在“总收入”和“总扣除”中"

4

3 回答 3

3

您只需要为文本框设置 keyup 功能。

 <script type="text/javascript">
   function setval()
   {
      document.getElementById('totearng').value = document.getElementById('earng').value;
      document.getElementById('totdedtn').value = document.getElementById('dedtn').value;
   }
 </script>
 <body>
     Earning : <input type="text" id='earng' onkeyup="setval()"/><br/>
     Deduction : <input type="text" id='dedtn' onkeyup="setval()"/><br/>
     Total Earning : <input type="text" id='totearng'/><br/>
     Total Deduction : <input type="text" id='totdedtn'/>
 </body>
于 2013-09-19T13:22:19.887 回答
0

use javascript for calculation

function cal()
{
//calculation part here
//var total_earning = your result

//Earnings textbox
document.getElementById(textfield_name1).value = total_earning;

//Deductionstextbox
document.getElementById(textfield_name2).value = total_earning;
}
于 2013-09-19T12:56:10.240 回答
0
<!DOCTYPE html>
<html>
<head>
<script>
function displayResult()
{
    var x=(document.getElementById("deduction").value);
    var y=(document.getElementById("collection").value);
    document.getElementById("total").value=Number(x)+Number(y);
}
</script>
</head>
<body>

<form>
    Deduction: <input type="text" id="deduction" onchange="displayResult()">
    Collection: <input type="text" id="collection" onchange="displayResult()">
    Total:<input type="text" id="total">
</form>
</body>
</html>

像这样试试

于 2013-09-19T12:58:55.620 回答