如何使用js和jQuery从select元素中提取选中的value值?
- 内容介绍
- 文章标签
- 相关推荐
本文共计159个文字,预计阅读时间需要1分钟。
plaintextgistfile1.txttext1text2code:
1: JavaScript原生方法
2: 获取select对象:
3: 获取选中项的索引:
var index=myselect.selectedIndex; // selectedIndex表示所选项的索引 gistfile1.txtcode: 一:javascript原生的方法 1:拿到select对象: var myselect=document.getElementById("test"); 2:拿到选中项的索引:var index=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()); //拿到选中项的文本
本文共计159个文字,预计阅读时间需要1分钟。
plaintextgistfile1.txttext1text2code:
1: JavaScript原生方法
2: 获取select对象:
3: 获取选中项的索引:
var index=myselect.selectedIndex; // selectedIndex表示所选项的索引 gistfile1.txtcode: 一:javascript原生的方法 1:拿到select对象: var myselect=document.getElementById("test"); 2:拿到选中项的索引:var index=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()); //拿到选中项的文本

