[英]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. 我想为一个唯一的ID分配一个下拉选项,以便在选择该ID时,JavaScript将采用该ID,并将其转换为变量,我可以用它来显示成本。
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. 我无法显示价格,即使选择BN1时我只是试图将其打印为“你好”,也不起作用。 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.