简体   繁体   English

自动百分比计算器

[英]Automatic percentage calculator

I am working on a code to calculate percentage but I need it to calculate the percentage automatically without the submit button, that is I want it to output the calculated percentage without the need of the user hitting the submit button. 我正在编写用于计算百分比的代码,但是我需要它来自动计算百分比,而不需要提交按钮,也就是说,我希望它输出计算出的百分比而无需用户点击提交按钮。

Here is my code 这是我的代码

Input: <input type="text" id="input"/><br />
Percent: <input type="text" id="percent" value="50"/>%<br />
Output: <input type="text" id="output"/>


function calc() {
  var i = document.getElementById("input").value;
  var p = document.getElementById("percent").value;
  var o = (i/100) * p;
  document.getElementById("output").value = o;
}

On each of the two inputs, add onchange="calc();" 在两个输入的每一个上,添加onchange="calc();" (updates when user tabs to the next field) or onkeyup="calc();" (当用户onkeyup="calc();"下一个字段时更新)或onkeyup="calc();" (updates whenever user types something). (每当用户键入内容时更新)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM