JS获取select值和索引的方法
2016-12-16 17:13
183 查看
分别使用javascript原生的方法和jquery方法
<select id="test" name="">
<option value="1">text1</option>
<option value="2">text2</option>
</select>
code:
一:javascript原生的方法
1:拿到select对象: var myselect=document.getElementById("test");
2:拿到选中项的索引:varindex=myselect.selectedIndex ; //selectedIndex代表的是你所选中项的index
3:拿到选中项options的value: myselect.options[index].value;
4:拿到选中项options的text: myselect.options[index].text;
二:jquery方法(前提是已经加载了jquery库)
1:var options=$("#test option:selected"); //获取选中的项
2:alert(options.val()); //拿到选中项的值
3:alert(options.text()); //拿到选中项的文本
<select id="test" name="">
<option value="1">text1</option>
<option value="2">text2</option>
</select>
code:
一:javascript原生的方法
1:拿到select对象: var myselect=document.getElementById("test");
2:拿到选中项的索引:varindex=myselect.selectedIndex ; //selectedIndex代表的是你所选中项的index
3:拿到选中项options的value: myselect.options[index].value;
4:拿到选中项options的text: myselect.options[index].text;
二:jquery方法(前提是已经加载了jquery库)
1:var options=$("#test option:selected"); //获取选中的项
2:alert(options.val()); //拿到选中项的值
3:alert(options.text()); //拿到选中项的文本
相关文章推荐
- 关于JS获取select值的两种实现方法
- JS获取select-option-text_value的方法
- jquery和js方法 获取text和radio select值 及是否选中
- 关于JS获取select值的两种实现方法
- 关于JS获取select值的两种实现方法
- JS获取select-option-text_value的方法
- JS,jQuery获取select标签中选中值的方法
- select各种获取值设置值得方法 js和jquery
- 《JS操作select相关方法》
- JS操作select相关方法:新增 修改 删除 选中 清空 判断存在 等
- JS操作select相关方法:新增 修改 删除 选中 清空 判断存在 等
- js的request方法,对“获取静态文件传递参数的函数” 进行了优化
- [收藏]JS获取网页中HTML元素的几种方法分析
- JS操作select相关方法
- 两个很简单的js获取url参数的方法
- js类中获取外部函数名的方法
- js操作select相关方法(收集)
- 一个JS获取url参数的方法
- JS操作select相关方法
- JS操作select相关方法