我的代码中的错误是什么?变量的乘法没有显示在总数中

What is the error in my code? The multiplication of my variables is not being displayed in the total

本文关键字:显示 变量 错误 代码 是什么 我的      更新时间:2023-09-26

我似乎无法让totalCost变量显示相乘的值。我认为id或价格变量可能有问题。是否有更好的方法来编写这段代码?

<!DOCTYPE html>
  <html>
    <head>    
       <Script type="text/javascript">    
          function multiply() {    
            var hoursParked = document.getElementById('hP').value;
            var price = 5.00;
            var totalCost = document.getElementById('total');
            var totalPayment = (hoursParked * price);
            totalCost.value= totalPayment;
          }    
      </Script>
   </head>
   <body>
      <div align="center">
        <form id="1">
            <h1><p style="background-color:blue ; color:red;">Jamie's     Parking Garage Payment Calculator</p></h1><hr>
            <input type="text" id="customerName" name="customerName" placeholder="Please enter your name here." size="27px"><br><br>
            <input type="number" id="hP" name="hoursParked" placeholder="Hours parked?" min="1" max="24"><br><br>
            <input type="button" id="calculateP" name="calculatePayment" value="Calculate" onclick="multiply()"/>
            <input type="reset" id="resetBtn" value="Clear"><br><br>
            <input type="number" id="total" name="totalCost" placeholder="Your Total Payment" readonly/><hr>
        </form>
      </div>
   </body>
</html>

您没有关闭函数。在</Script>之前添加}。你也应该在你的标签</script>中使用小写。