简体   繁体   English

如何使用JavaScript自动计算金额

[英]How to calculate the amount automatically using javascript

This is my function 这是我的职责

function  doMath() { 
   var counter; var nvalue; var amount;
   var price=100;
   nValue = document.getElementById("message").value;
   amount=(nvalue*price);
   document.getElementById("total").value=amount ;
}

My html 我的HTML

<input type="" name="message" id="message" onkeyup="doMath()"maxlength="60">message
<input type="text" name="total" id="total" maxlength="60"> amount

when user enter value into message field it should calculate the amount automatically and show it in amount field. 当用户在消息字段中输入值时,应自动计算金额并将其显示在金额字段中。

but while i entering the value it show NAN 但是当我输入值时它显示NAN

its not calculating can anyone help me how to fix this .im new to javascript 它没有计算任何人可以帮助我如何解决此.im javascript新问题

You declared your variable as nvalue but used nValue instead. 您将变量声明为nvalue,但改用了nValue

   function  doMath() { 
       var nValue; var amount;
       var price=100;
       nValue = document.getElementById("message").value;
       amount=(nValue*price);
       document.getElementById("total").value=amount ;
    }

Your error is here: 您的错误在这里:

  amount = (nvalue * price);

It should be: 它应该是:

  amount = (nValue * price);

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

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