使用javascript获取选择的值 [英] Getting a select's value with javascript
本文介绍了使用javascript获取选择的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下简明格式
<form name="thecars">
<select name="cars">
<option value="mustang">Mustang</option>
<option value="pinto">Pinto</option>
<option value="pinto">Chevelle</option>
<option value="pinto">Other</option>
</select>
</form>
我正在尝试通过以下方法获得所选汽车的价值,但它不起作用
I am trying to get the value of the selected car by the following but it is not working
selectedCar = document.forms["thecars"].elements["cars"].options[thecars.cars.options.selectedIndex].value;
推荐答案
正确的代码为:
var oForm = document.forms["thecars"];
var oDDL = oForm.elements["cars"];
var selectedCar = oDDL.value;
仅使用表单名称就无法获得对该表单的引用.
You can't get reference to the form by just using its name.
这篇关于使用javascript获取选择的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文