如何获得select被选中option的value和text和其他属性值

来源:互联网 发布:mac关闭icloud drive 编辑:程序博客网 时间:2024/05/16 17:22

我想获取select选中的value,或者text,或者……

比如这个:

<select id="select">    <option value="A" url="http://www.baidu.com">第一个option</option>    <option value="B" url="http://www.qq.com">第二个option</option></select>

一:JavaScript原生的方法

1:拿到select对象: var myselect=document.getElementById("select");2:拿到选中项的索引:var index=myselect.selectedIndex ; // selectedIndex代表的是你所选中项的index3:拿到选中项options的value: myselect.options[index].value;4:拿到选中项options的text: myselect.options[index].text;5:拿到选中项的其他值,比如这里的url: myselect.options[index].getAttribute('url');

二:jQuery方法

1:var options=$("#select option:selected"); //获取选中的项2:alert(options.val()); //拿到选中项的值3:alert(options.text()); //拿到选中项的文本4:alert(options.attr('url')); //拿到选中项的url值
阅读全文
0 0
原创粉丝点击