如何获得select被选中option的value和text

来源:互联网 发布:淘宝玩具古剑 编辑:程序博客网 时间:2024/05/21 14:48

一:JavaScript原生的方法

拿到select对象: var myselect=document.getElementById(“test”);拿到选中项的索引:var index=myselect.selectedIndex ; // selectedIndex代表的是你所选中项的index拿到选中项options的value: myselect.options[index].value;拿到选中项options的text: myselect.options[index].text;

二:jQuery方法(前提是已经加载了jquery库)

var options=$(“#test option:selected”); //获取选中的项alert(options.val()); //拿到选中项的值alert(options.text()); //拿到选中项的文本
阅读全文
0 0