js获取当前select元素值的代码_表单特效
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了js获取当前select元素值的代码_表单特效,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1398字,纯文字阅读大概需要2分钟。
内容图文
![js获取当前select元素值的代码_表单特效](/upload/InfoBanner/zyjiaocheng/311/79218acf5cd646cc99e7770af593f53f.jpg)
2、可以通过 select.selectedIndex 获取到选中的 option 元素的索引。
3、可以通过 select.options[select.selectedIndex] 获取到选中的 option 元素。
4、option 元素 ,可以通过 option.value 获得 option 元素的 value 属性值,即 value3;可以通过 option.text 获得 option 元素内的文本,即 text3。
5、如果 option 元素没有定义 value 属性,则 IE 中 option.value 无法获得,但 Safari、Opera、FireFox 依旧可以通过 option.value 获得,值同于 option.text 。
6、可以通过 option.attributes.value && option.attributes.value.specified 来判断 option 元素是否定义了 value 属性。
故,获得当前 select 元素值的脚本如下:
代码如下:
var getSelectValue = funtion(select) {
var idx = select.selectedIndex,
option,
value;
if (idx > -1) {
option = select.options[idx];
value = option.attributes.value;
return (value && value.specified) ? option.value : option.text);
}
return null;
}
以前由于兼容性问题,大家用select.options[select.sekectedIndex].value来取值,不过现在用select.value都可以了