简体   繁体   English

价格计算器使用表格

[英]Price Calculator Using Forms

So here's the thing, I want to make a price calculator by letting the user fill up the form but it won't work. 这就是问题,我想通过让用户填写表格来制作价格计算器,但无法正常工作。 Also, I wanted to show the result on the input field. 另外,我想在输入字段上显示结果。 Thank you in advance. 先感谢您。

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

mistakes 错误

  1. Please change getElementByID by getElementById. 请通过getElementById更改getElementByID。
  2. parseInt only parse value in integer so you multiplication give you only integer value so you have to parse in float. parseInt仅解析整数形式的值,因此乘法只能提供整数值,因此您必须以float形式进行解析。 it's give you perfect output. 它给您完美的输出。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM