1

正如标题所说,我需要创建一个简单的总价格函数。我需要将人数乘以 1000 并在“totalPrice”中显示。任何 javascript/html/jquery 都很棒。到目前为止我有这个:

 <html>
 <body>
     <form id="buyTicket" method="post">
         <div>
             Booking Name : <input type="text" required id="bookingName" placeholder="booking name" /> 
         </div>
         <div>
             Number of persons : <input type="number" required id="numberOP" placeholder="number of persons"/> 
         </div>
         <div>
             Depature : <input type="text" required id="depature" class="datepicker" placeholder="enter depature date"/> 
         </div>
         <div>
             Return           :
             <input type="text" required id="return" class="datepicker" placeholder="enter return date"/> 
         </div>
         <div id="totalPrice">Total:</div>
         <div>
             <button type="submit" id="buttonTicket">Buy Ticket</button>
         </div>
     </form>
     <script type="text/javascript"> 
        function CalculateTotal() {
            var numberOP = document.getElementById('numberOP').value;
            var total = numberOP * 1000;                       
            document.getElementById('totalPrice').innerHTML = total;
        }
    </script>
 </body>
 </html>
4

4 回答 4

1

您需要调用您的函数来显示总价。将更改事件添加到您的输入以调用函数。

<div>Number of persons :<input type="number" required id="numberOP" onchange="CalculateTotal()" placeholder="number of persons"/></div>

完整代码:

<html><body>
   <form id="buyTicket" method="post">
      <div>Booking Name      :<input type="text" required id="bookingName" placeholder="booking name" /></div>
      <div>Number of persons :<input type="number" required id="numberOP" onchange="CalculateTotal()" placeholder="number of persons"/></div>
      <div>Depature          :<input type="text" required id="depature" class="datepicker" placeholder="enter depature date"/></div>
      <div>Return            :<input type="text" required id="return" class="datepicker" placeholder="enter return date"/></div>
      <div id="totalPrice">Total:</div>
      <input type="hidden" id="total" /> <!-- BAD IDEA -->
      <div><button type="submit" id="buttonTicket">Buy Ticket</button></div>
   </form>

   <script type="text/javascript"> 
      function CalculateTotal() {
         var numberOP = document.getElementById('numberOP').value;
         var total = numberOP * 1000;                       
         document.getElementById('totalPrice').innerHTML = 'Total: ' + total;
         document.getElementById('total').value = total; //<--BAD IDEA
      }
   </script>
</body></html>
于 2012-05-31T06:54:02.630 回答
1

尝试 parseInt 获取数字而不是字符串。

function CalculateTotal()
{ 
       var numberOP =  parseInt("0" + document.getElementById('numberOP').value);
       var total = numberOP * 1000;                       
       document.getElementById('totalPrice').innerHTML = total;
}

//Call above method
CalculateTotal();

你可以在提交按钮上调用这个方法来检查它是否有效

 <button type="submit" id="buttonTicket" onclick="CalculateTotal();" >Buy Ticket</button>
于 2012-05-31T06:54:05.883 回答
1

我认为您不需要在您的情况下使用表单。您只想显示总价。试试这个:

<html>
<body>
     <div>
         Booking Name : <input type="text" required id="bookingName" placeholder="booking name" /> 
     </div>
     <div>
         Number of persons : <input type="number" required id="numberOP" placeholder="number of persons"/> 
     </div>
     <div>
         Depature : <input type="text" required id="depature" class="datepicker" placeholder="enter depature date"/> 
     </div>
     <div>
         Return           :
         <input type="text" required id="return" class="datepicker" placeholder="enter return date"/> 
     </div>
     <div id="totalPrice">Total:</div>
     <div>
         <button type="submit" id="buttonTicket" onlick="CalculateTotal();">Buy Ticket</button>
     </div>
 <script type="text/javascript"> 
    function CalculateTotal() {
        var numberOP = document.getElementById('numberOP').value;
        var total = numberOP * 1000;                       
        document.getElementById('totalPrice').innerHTML = total;
    }
</script>
</body>
</html>
于 2012-05-31T07:03:20.727 回答
0

Knockout.js 就是为这种情况而设计的,请参阅Knockout 主页

于 2012-05-31T07:08:13.027 回答