Javascript订单总计咖啡无法获得功能。请协助

时间:2015-04-09 06:03:13

标签: javascript html function

好吧,这就是我作业的javascript部分的任务。

Javascript函数将:

  1. 取三个参数:名称,饮品类型和额外拍摄数量
  2. 检查以确定是否给出了名称,并在没有名称的情况下提醒用户
  3. 根据订购的类型计算饮料价格
  4. 根据订购的额外拍摄数量增加额外费用
  5. 在计算饮料总量时提醒用户。它将告知用户订购的饮料,额外拍摄的数量以及订单的总成本。
  6. 以下是您编程所需的信息:

    1. 可用的饮料和价格a。拿铁= $ 3.00 b。 Americano = $ 2.00 c。卡布奇诺= 2.50美元。滴滤咖啡= $ 1.50
    2. 额外的浓缩咖啡每张0.5美元。最多只能订购4张额外照片
    3. 继承我的代码,我无法获得我已经尝试了几个小时的javascript部分。

      <!DOCTYPE html>
      <html>
      <head>
      <script type="text/javascript"> 
      function first(drink, shots, customer) { 
          var x = customer.value; 
          if (x == '' || x == 'null') { 
              alert("Yo enter a name."); 
          }
          else { 
              if (drink == "0") { 
                  price = 3; 
              } else if (drink == "1") { 
                  price = 2; 
              } else if (drink == "2") { 
                  price = 2.50; 
              } else { 
                  price = 1.50; 
              } 
              if(shots > 0) { 
                  Price= price + (shots * 0.5) 
              } else { 
                  alert ("Thank you"); 
              } 
          }
      }
      </script>
      </head>
      <body>
      <form>
      <h1>Javascript calculating Function: Calculate the cost of a coffee drinks using Javascript.</h1>
      <p>What would you like to order?</p>
      
      <select id="drink">
      
          <option value="0" id="0">Latte</option>
          <option value="1" id="1">Americano</option>
          <option value="2" id="2">Cappuccino</option>
          <option value="3" id="3">Drip Coffee</option>
      
      </select>
      
      <br>
      <br>
      
      <select id="shots">
      
          <option value="0" id="0">No Extra</option>
          <option value="1" id="1">1</option>
          <option value="2" id="2">2</option>
          <option value="3" id="3">3</option>
          <option value="4" id="4">4</option>
      
      </select>
      
      <br>
      <br>
      
      <p>Enter name here:</p>
      
      <input id="customer" value="customer" type="text"/>
      
      <br>
      
       <br>
      
          <button onclick="first(drink, shots, customer)">Complete Order</button>
      
          </form>
          </body>
          </html>
      

      任何帮助都表示感谢,提前谢谢!

2 个答案:

答案 0 :(得分:0)

您需要从输入字段中获取first()的参数。

<button onclick="first(document.getElementById('drink').value, document.getElementById('shots').value, document.getElementById('customer'))">Complete Order</button>

答案 1 :(得分:0)

试试这个

<h1>Javascript calculating Function: Calculate the cost of a coffee drinks using Javascript.</h1>

您想订购什么?

<option value="0" id="0">Latte</option>
<option value="1" id="1">Americano</option>
<option value="2" id="2">Cappuccino</option>
<option value="3" id="3">Drip Coffee</option>



<option value="0" id="0">No Extra</option>
<option value="1" id="1">1</option>
<option value="2" id="2">2</option>
<option value="3" id="3">3</option>
<option value="4" id="4">4</option>



在此输入姓名:



<button id='submit'>Complete Order</button>
<script>
$("#submit").click(function () 
{
    var drink = $("#drink").val();
    var shots = $("#shots").val();
    var customer = $("#customer").val();

    var x = customer; 
    if (x == '' || x == 'null') 
    { 
        alert("Yo enter a name."); 
    }
   else 
   { 
       if (drink == "0") { price = 3; } 
       else if (drink == "1") { price = 2; } 
       else if (drink == "2") { price = 2.50; } 
       else { price = 1.50; } 
       if(shots > 0) 
       {
           price= price + (shots * 0.5) 
       } 
       alert ("Your Order Total is : "+price+" \nThank you"); 

   }
});
</script>