如何使用javascript中的html选项中的输入?

时间:2015-03-04 18:29:20

标签: javascript html

我想为一个唯一的ID分配一个下拉选项,这样当它被选中时,javascript将获取该ID并将其转换为一个变量,我可以用它来显示成本。

这是我到目前为止所做的:

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

            }

我不能让它显示价格,即使我只是试图让它打印出“你好”当选择BN1它不起作用。谁能告诉我我错过了什么?

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
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;

            }
&#13;
<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>
&#13;
&#13;
&#13;