Javascript 在javascript中计算两个输入字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28985656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:42:04  来源:igfitidea点击:

calculate two input field values in javascript

javascripthtml

提问by Justin Jose

Hi i want to calculate two input field values and result will show in third input field so i want to write code in ajax page

嗨,我想计算两个输入字段值,结果将显示在第三个输入字段中,所以我想在 ajax 页面中编写代码

<input id="a1" type="text" />
<input id="a2" type="text" onblur="Calculate();"  />
<input id="a3" type="text" name="total_amt" value="" />

here javascript function

这里 javascript 函数

 <script>


function Calculate()
{
var resources = document.getElementById('a1').value;
var minutes = document.getElementById('a2').value; 
document.getElementById('a3').value=parseInt(resources) *       parseInt(minutes);
 document.form1.submit();
   }
 </script>

starting its working but nw its not working please help me

开始工作但不工作请帮助我

Thanks in Advance

提前致谢

回答by Maybe Gordon

Look this! Work it. http://jsfiddle.net/op1u4ht7/2/

看看这个!解决它。 http://jsfiddle.net/op1u4ht7/2/

<input id="a1" type="text" />
<input id="a2" type="text" onblur="calculate()"  />
<input id="a3" type="text" name="total_amt" />

calculate = function()
{
    var resources = document.getElementById('a1').value;
    var minutes = document.getElementById('a2').value; 
    document.getElementById('a3').value = parseInt(resources)*parseInt(minutes);

   }

回答by Justin Jose

Try AutoCalculator https://github.com/JavscriptLab/autocalculateCalculate Inputs value and Output By using selector expressions

尝试 AutoCalculator https://github.com/JavscriptLab/autocalculate使用选择器表达式计算输入值和输出

Just add an attribute for your output input like data-ac="(#firstinput+#secondinput)"

只需为您的输出输入添加一个属性,例如 data-ac="(#firstinput+#secondinput)"

No Need of any initialization just add data-ac attribute only. It will find out dynamically added elements automatically

无需任何初始化,只需添加 data-ac 属性即可。它会自动找出动态添加的元素

FOr add 'Rs' with Output just add inside curly bracket data-ac="{Rs}(#firstinput+#secondinput)"

For add 'Rs' with Output 只需在大括号内添加 data-ac="{Rs}(#firstinput+#secondinput)"