首页 > 解决方案 > 我试图建立一个网站,javascript数学不起作用

问题描述

我正在尝试使用 codepen.io 制作一个网站来销售东西(不是真正的“销售”东西),但由于某种原因,使用 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;
}

标签: javascriptcalculation

解决方案


'$14' is not a number.

computers are perfect at math. that's their primary 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.

try adding console.log() statements to your code so you can see what variable values you are dealing with.


推荐阅读