日期:2014-05-16  浏览次数:20373 次

js 如何获取options选定的值
<select id="add"><option value="choose" selected="selected">请选择</option><option value="0">1店</option><option value="1">2店</option><option value="2">3店</option></select>


用以下代码提取 option 值会报错,请问原因是什么?

document.getElementById("add").onchange = function (f) {
f = f || window.event;
var g = f.target || f.srcElement;
var selectText = document.getElementById("add").options[index].text;

------解决方案--------------------
var selectText = document.getElementById("add").options[index].text;

这句代码里,你的index值在哪?

var selectValue = document.getElementById("add").value;  //获取选中option的value值
var index = document.getElementById("add").selectedIndex;//获取第几个option被选中
var selectText = document.getElementById("add").options[index].text;//获取被选中中的option的显示值。
console.log(selectValue);
console.log(index);
console.log(selectText);