咖啡的Javascript订单总数无法使函数正常工作.请协助

Javascript order total for coffee cant get function to work. PLEASE ASSIST

本文关键字:常工作 工作 函数 Javascript 单总数 咖啡      更新时间:2023-09-26

好的,这是我家庭作业的javascript部分的任务。

Javascript 函数将:

  1. 取三个参数:名称、饮料类型和额外拍摄次数
  2. 检查以确保给定了名称,如果没有名称则提醒用户
  3. 根据订购的类型计算饮料价格
  4. 根据订购的额外拍摄次数添加额外费用
  5. 在计算饮料总量时提醒用户。它将通知用户订购的饮料、额外拍摄次数以及订单的总成本。

以下是编程所需的信息:

  1. 可用饮料和价格 a. 拿铁 = $3.00 b. 美式咖啡 = $2.00 c. 卡布奇诺 = $2.50 d. 滴漏咖啡 = $1.50
  2. 额外的浓缩咖啡每杯 50 美元。最多只能订购 4 个额外的镜头

这是我的代码,我无法获得我已经尝试了几个小时的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>

任何帮助,非常感谢,提前感谢您!

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

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

试试这个

<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>