简体   繁体   English

我试图建立一个网站,javascript 数学不起作用

[英]Im trying to make a website, javascript math doesn't work

I'm trying to use codepen.io to make a website to sell things (not really "sell" things), but for some reason the calculations with the javascript won't work, I'm trying to make it calculate the total and display it in a text-field.我正在尝试使用 codepen.io 制作网站来销售东西(不是真正“销售”东西),但由于某种原因,使用 javascript 的计算不起作用,我试图让它计算总数和将其显示在文本字段中。 Here is the javascript.这是 javascript。

function doFunction(){

   var shirtNo, bearNo, pillowNo, totalNo; 
   var shirtNo =  document.getElementById("panda_shirt").value;
   bearNo = document.getElementById("panda_toy").value;
   pillowNo =    document.getElementById("panda_pillow").value;
   var totalQty = panda_shirt + panda_toy + panda_pillow ,
     estimateTotal;
   estimateTotal = ($14 * panda_shirt) + ($12 * panda_toy) + ($10 * 
   panda_pillow);
  
   document.getElementById('estimateTotal').value = Estimate Total;
}

You had some syntax errors.你有一些语法错误。 Try the code below.试试下面的代码。 This will give you the total price, then you can use it however you'd like.这将为您提供总价格,然后您可以随意使用它。

  function doFunction(){ 
     var shirtNo =  document.getElementById("panda_shirt").value;
     var bearNo = document.getElementById("panda_toy").value;
     var pillowNo = document.getElementById("panda_pillow").value;
     var estimateTotal = (14 * Number(shirtNo)) + (12 * Number(bearNo)) + (10 * Number(pillowNo));

'$14' is not a number. “$14”不是数字。

computers are perfect at math.计算机在数学方面是完美的。 that's their primary function.那是他们的主要 function。 so if the math is wrong, it's your code, not the computer or the language.所以如果数学是错误的,那是你的代码,而不是计算机或语言。

also you have not defined panda_shirt or your other variables, so it appears you are attempting to do math with undefined objects.您还没有定义panda_shirt或其他变量,因此您似乎正在尝试对未定义的对象进行数学运算。

try adding console.log() statements to your code so you can see what variable values you are dealing with.尝试将console.log()语句添加到您的代码中,以便您可以查看正在处理的变量值。

Javascript is a dynamically typed language. Javascript 是一种动态类型语言。 The type of a variable is defined by the value it contains.变量的类型由它包含的值定义。

So, In your code, the variables shirtNo , bearNo , pillowNo most probably contain string values - because they seems to be read from HTML text elements.因此,在您的代码中,变量shirtNobearNopillowNo很可能包含字符串值——因为它们似乎是从 HTML 文本元素中读取的。

In order to perform mathematical operations, the variables must contain number values.为了执行数学运算,变量必须包含number You can convert a string value to a number by using the Number function.您可以使用Number function 将字符串值转换为数字。

Number(shirtNo)

Also, $14 will not give it a Dollar value.此外, $14不会给它一个美元价值。 A number is just a number in Javascript.数字只是 Javascript 中的数字。 The currency is only an interpretation.货币只是一种解释。

So, you can rewrite your code like this:因此,您可以像这样重写代码:

 function doFunction(){

   var shirtNo, bearNo, c; 

   shirtNo =  document.getElementById("panda_shirt").value;
   bearNo = document.getElementById("panda_toy").value;
   pillowNo = document.getElementById("panda_pillow").value;


   estimateTotal = (14 * Number(shirtNo)) + (12 * Number(bearNo)) + (10 * Number(pillowNo));
  
   document.getElementById('estimateTotal').value = estimateTotal;
 }

However, it may not be that the user has entered a numerical value in the inputs.但是,可能不是用户在输入中输入了数值。 In that case the function isNaN() can tell you if any value is Not A Number.在这种情况下,function isNaN()可以告诉您是否有任何值不是数字。

You need to convert the value of bearNo to an int or float number.您需要将BearNo的值转换为intfloat Otherwise, it will be count as a string.否则,它将被视为一个字符串。 And $14 doesn't work like this. 14 美元不是这样工作的。 You can't use a $ sign when multiplying with numbers.与数字相乘时不能使用 $ 符号。

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

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