简体   繁体   中英

if statement from drop down selection and then save data

I have a box for user to enter altitude number and a drop down box right next to it so the user has option to choose "feet" or "meters". I don't know how to make an if statement that will recognize if the drop down box option was chosen in feet, it will then convert to Meters (I want to have meters as default unit in the system but still give user option to enter in feet if needed to)and then save the database.

Altitude : <input type = "text" name = "Altitude" value = "0" select id = "altUnits" name = "AltUnits"> <option value = "FEET" > FEET options  <option value = "METERS" > METERS options select

input type = <"submit" tabindex = 2 name = "jdbc_query_add_Newpoint" value = "ADD"/>

this should get you started.

 document.getElementById("mySelect").addEventListener("change", function() { var mySelect = document.getElementById('mySelect').value; if(mySelect === "FEET"){ var input = document.getElementById('altUnits'); input.value = input.value*.3048; } })
 Altitude : <input type = "text" name = "Altitude" value = "0" select id = "altUnits" name = "AltUnits"> <select id='mySelect' > <option></option> <option value = "FEET" > FEET options </option> <option value = "METERS" > METERS options select</option> </select> <input type = "submit" tabindex = 2 name = "jdbc_query_add_Newpoint" value = "ADD"/>

Please refer below code

 $(document).ready(function() { var altitude = $("#altUnits").val(); var unit = $("#unit").val(); var result = convert(unit, altitude) result += " " + unit $("#result").text(result); $("#unit").change(function() { var altitude = $("#altUnits").val(); var unit = $("#unit").val(); result = convert(unit, altitude); result += " " + unit $("#result").text(result); }); $("#altUnits").change(function() { var altitude = $("#altUnits").val(); var unit = $("#unit").val(); result = convert(unit, altitude); result += " " + unit $("#result").text(result); }); }); function convert(conversionUnit, value) { var res = "" switch (conversionUnit) { case "Meters": res = 0.3048 * value; break; case "Feet": res = 3.2808 * value break; } return res; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <lable>Altitude: </lable> <input type = "text" name = "Altitude" value = "0" select id = "altUnits" name = "AltUnits"> <lable>Unit: </lable> <select id="unit"> <option>Meters</option> <option>Feet</option> </select> <lable>Conversion : <span id="result"></span></lable>

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