加入收藏 | 设为首页 | 会员中心 | 我要投稿 河北网 (https://www.hebeiwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 正文

JS 如何获取当前 select 元素的值

发布时间:2018-09-07 21:30:14 所属栏目:创业 来源:站长网
导读:假如 select 元素下的全部 option 元素均没有指定 selected 属性,会默认选中第一个。 可以通过 select.selectedIndex 获取到选中的 option 元素的索引。 可以通过 select.options[select.selectedIndex] 获取到选中的 option 元素。 option 元素 text3,

假如 select 元素下的全部 option 元素均没有指定 selected 属性,会默认选中第一个。

可以通过 select.selectedIndex 获取到选中的 option 元素的索引。

可以通过 select.options[select.selectedIndex] 获取到选中的 option 元素。

option 元素 text3,可以通过 option.value 得到 option 元素的 value 属性值,即 value3;可以通过 option.text 得到 option 元素内的文本,即 text3。

假如 option 元素没有界说 value 属性,则 IE 中 option.value 无法得到,但 SafariOpera、FireFox 仍旧可以通过 option.value 得到,值同于 option.text 。

可以通过 option.attributes.value && option.attributes.value.specified 来判定 option 元素是否界说了 value 属性。

故,获适合前 select 元素值的剧本如下:

Copy to ClipboardLiehuo.Net Codes引用的内容:[www.veryhuo.com] 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;
}

原文:http://www.planabc.net/2010/03/27/how_to_get_select_element_value/

(编辑:河北网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读