1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > js获取当前select 元素值的代码_表单特效

js获取当前select 元素值的代码_表单特效

时间:2022-11-18 18:51:46

相关推荐

js获取当前select 元素值的代码_表单特效

web前端|js教程

js,select,元素值

web前端-js教程

如果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 无法获得,但 Safari、Opera、FireFox 依旧可以通过 option.value 获得,值同于 option.text 。

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

故,获得当前 select 元素值的脚本如下:

linux 小游戏源码,ubuntu 如何看时区,py爬虫语言基础,PHP怎么访问其他PHP,前端seo书籍lzw

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;

}

mac eclipse jdk 源码,ubuntu压缩目录结构,爬虫为啥要sleep,学习php和php,seo网站基础lzw

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。