简体   繁体   中英

How to use an input from a select option in html with javascript?

I want to assign a drop down option to a unique ID so that when it is selected, the javascript will take that ID and turn it into a variable which I can use to display the cost.

This is what I have so far:

<form>
        <select>
        <option name ="Bristol "value="40.0" id="BN1">Bristol - Newcastle</option>
        </select>
        <button type="button" onclick="BookingFare(); return false;">Submit</button><br>
        </form>

function BookingFare() { 


            var price = 0;
            //var ofAdults = document.getElementById('adults').value;
            //var ofChildren = document.getElementById('children').value;
            var BN1 = document.getElementById('BN1').value;

            if (BN1) {
             var price = price + 40; 
            }

            document.getElementById('priceBox').innerHTML = price;

            }

I can't get it to display the price, even when I just try to get it to print out "hello" when BN1 is selected it doesn't work. Can anyone tell me what I'm missing?

 function BookingFare() { var price = 0; //var ofAdults = getElementById('adults').value; //var ofChildren = getElementById('children').value; var BN1 = document.getElementById('BN1').value; if (BN1) { var price = price + 40; } document.getElementById('priceBox').innerHTML = price; } 
 <form> <select id="BN1"> <option name ="Bristol "value="40.0" >Bristol - Newcastle</option> </select> <button type="button" onclick="BookingFare(); return false;">Submit</button><br> </form> <label id='priceBox'></label> 

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