简体   繁体   中英

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.
  2. parseInt only parse value in integer so you multiplication give you only integer value so you have to parse in 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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