gpt4 book ai didi

javascript - 使用表格的价格计算器

转载 作者:行者123 更新时间:2023-11-28 02:58:14 25 4
gpt4 key购买 nike

事情是这样的,我想通过让用户填写表格来制作价格计算器,但它行不通。另外,我想在输入字段中显示结果。提前谢谢你。

<!doctype HTML>
<html lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>

</title>
<head>

</head>
<body>
<form role="form" id="price" name="price">
<select class="" id="itemselect" onchange="calculate()">
<option id="itemprice" value="1.500">Adult's T-Shirt</option>
<option id="itemprice" value="1.250">Kid's T-Shirt</option>
<option id="itemprice" value="3.000">Dubai Polo</option>
<option id="itemprice" value="6.000">Aerocool Polo</option>
<option id="itemprice" value="4.000">Aerocool Crewneck</option>
<option id="itemprice" value="5.000">Hoodies</option>
</select>
<p><input type="text" id="qty" onchange="calculate()" value=""></p>
<p><input type="text" id="result" value="" disabled></p>
</form>

<script type="text/javascript">
function calculate(price){
var item = document.getElementByID("itemselect").value;
var qty = document.getElementByID("qty");

item = parseInt(item);
qty = parseInt(qty);

var result = item*qty;

document.getElementByID("result").value=result;
}
</script>
</body>
</html>

最佳答案

错误

  1. 请将 getElementByID 更改为 getElementById。
  2. parseInt 只解析整数值,所以乘法只给你整数值,所以你必须解析 float 。它会给你完美的输出。

<!doctype HTML>
<html lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>

</title>
<head>

</head>
<body>
<form role="form" id="price" name="price">
<select class="" id="itemselect" onchange="calculate()">
<option id="itemprice" value="1.500">Adult's T-Shirt</option>
<option id="itemprice" value="1.250">Kid's T-Shirt</option>
<option id="itemprice" value="3.000">Dubai Polo</option>
<option id="itemprice" value="6.000">Aerocool Polo</option>
<option id="itemprice" value="4.000">Aerocool Crewneck</option>
<option id="itemprice" value="5.000">Hoodies</option>
</select>
<p><input type="text" id="qty" onchange="calculate()" value=""></p>
<p><input type="text" id="result" value="" disabled></p>
</form>

<script type="text/javascript">
function calculate(price){
var item = document.getElementById("itemselect").value || 0;

var qty = document.getElementById("qty").value || 0;

item = parseFloat(item).toFixed(2);
qty = parseFloat(qty).toFixed(2);

var result = parseFloat(item*qty).toFixed(2);

document.getElementById("result").value=result;
}
</script>
</body>
</html>

关于javascript - 使用表格的价格计算器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35916493/

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