gpt4 book ai didi

javascript - 需要帮助在本文档末尾显示计算结果

转载 作者:行者123 更新时间:2023-11-28 03:27:23 25 4
gpt4 key购买 nike

我试图在底部的文本框中显示“总计”,id'd为“输出”。我目前不明白如何在文本框中显示总应付款,因此任何帮助将不胜感激!另外,我想知道脚本标签中是否需要任何函数...在赋值标题下,它指出我应该只在脚本标签中声明变量。谢谢大家!

<!doctype html>
<html>
<head>
<meta charset = "UTF-8">
<title> Pizza Party</title>

</head>

<body>
<H1> PIZZA PLACE </H1>
<!--Image goes here-->
<center><img src="pizza.png" alt="Pizza" width="220px" height="160px"></center>
<br><br>



<script>
function pricecalculate()
{
var cheeseprice = 11.99*Number(cheese_value);
var mushroomprice = 12.99*Number(mushroom_value);
var sausageprice =13.49*Number(sausage_value);
var veggieprice =12.49*Number(veggie_value);
var deliver ="no";
var tip = 0;
var deliveryFee =0;
var priceofpizza = cheeseprice + mushroomprice + sausageprice + veggieprice

if (deliver="yes"){
deliveryFee =3.99;
}

output.value = totaldue
}


</script>
<p>Please designate the pizza type and the quantity in the boxes below</p>
<p>Cheese <input type ="text" id="cheese" onchange=''></p>
<p>Mushroom <input type ="text" id="mushroom" onchange=''></p>
<p>Sausage <input type ="text" id="sausage" onchange=''></p>
<p>Veggie <input type ="text" id="veggie" onchange=''></p>
<br><br>

<p>Delivery Preference</p>
<p><input type ="radio" name="takeout" id="takeout" onlick=''>Takeout</p>
<p><input type ="radio" name="delivery" id="delivery" onlick='deliver="yes"'>Delivery</p>

<br><br>
<p>Tip amount</p>
<p><input type ="radio" name="fifteen" id="fifteen" onlick='var tip=0.15'>15%</p>
<p><input type ="radio" name="eighteen" id="eighteen" onlick='var tip=0.18'>18%</p>
<p><input type ="radio" name="twenty" id="twenty" onlick='var tip=0.20'>20%</p>
<p><input type ="radio" name="notip" id="notip" onlick='var tip=0'>No Tip</p><!--All of the input boxes and directions to the user go below -->



<!--this is the start of the button info -->
<button id="totalSales" onclick = "
var totaldue = priceofpizza + (priceofpizza*tip) + deliveryFee
" >Calculate Total</button> <br>


<input type="text" id="output">
</body>
</html>

最佳答案

需要进行一些更改:

  • radio 组使用不正确;属于同一组的单选按钮需要具有相同的名称,否则可以为一组选择多个单选按钮

  • 您需要定义一个全局变量,以按照您的方式在不同范围之间传递值(例如 priceofpizza 不存在于您在 onclick 元素上使用的 <button> 范围内) ,我继续通过声明 global 来处理这个问题。/window名为 model 的变量

  • 您需要使用 DOM 实际检索输入的值 ( document )

<html><head>
<meta charset="UTF-8">
<title> Pizza Party</title>

</head>

<body>
<h1> PIZZA PLACE </h1>
<!--Image goes here-->
<center><img src="pizza.png" alt="Pizza" width="220px" height="160px"></center>
<br><br>



<script>
window.model = {};
window.pricecalculate = function()
{
model.cheeseprice = 11.99*Number(document.querySelector('#cheese').value);
model.mushroomprice = 12.99*Number(document.querySelector('#mushroom').value);
model.sausageprice =13.49*Number(document.querySelector('#sausage').value);
model.veggieprice =12.49*Number(document.querySelector('#veggie').value);
var deliveryOptions = Array.prototype.slice.call(document.getElementsByName('delivery-method')); // you need to use Array.prototype.slice.call to cast the NodeLits returned by document.getElementsByName into an Array and then use the Array's find method
console.log(deliveryOptions);
model.deliver =deliveryOptions.find(option => option.checked).value === 'delivery'
var tipOptions = Array.prototype.slice.call(document.getElementsByName('tip'));
model.tip = tipOptions.find(option => option.checked).value;

model.deliveryFee =0;
model.priceofpizza = model.cheeseprice + model.mushroomprice + model.sausageprice + model.veggieprice

if (deliver="yes"){
model.deliveryFee =3.99;
}

}


</script>
<p>Please designate the pizza type and the quantity in the boxes below</p>
<p>Cheese <input type="text" id="cheese" onchange=""></p>
<p>Mushroom <input type="text" id="mushroom" onchange=""></p>
<p>Sausage <input type="text" id="sausage" onchange=""></p>
<p>Veggie <input type="text" id="veggie" onchange=""></p>
<br><br>

<p>Delivery Preference</p>
<p><input type="radio" name="delivery-method" id="takeout" value="takeout">Takeout</p>
<p><input type="radio" name="delivery-method" id="delivery" value="delivery">Delivery</p>

<br><br>
<p>Tip amount</p>
<p><input type="radio" name="tip" id="fifteen" value="0.15">15%</p>
<p><input type="radio" name="tip" id="eighteen" value="0.18">18%</p>
<p><input type="radio" name="tip" id="twenty" value="0.20">20%</p>
<p><input type="radio" name="tip" id="notip" value="0">No Tip</p><!--All of the input boxes and directions to the user go below -->



<!--this is the start of the button info -->
<button id="totalSales" onclick="
pricecalculate();
var totaldue = model.priceofpizza + (model.priceofpizza*model.tip) + model.deliveryFee
var output = document.querySelector('#output');
output.value = totaldue;
">Calculate Total</button> <br>


<input type="text" id="output">

</body></html>

关于javascript - 需要帮助在本文档末尾显示计算结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58512608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com