加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 创业 > 模式 > 正文

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

发布时间:2018-09-09 13:27:46 所属栏目:模式 来源:站长网
导读:如果 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/

(编辑:PHP编程网 - 黄冈站长网)

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

    推荐文章
      热点阅读