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

Posted renrsh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何获得select被选中option的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>
  • 1
  • 2
  • 3
  • 4

一:javascript原生的方法

1:拿到select对象: var myselect=document.getElementById("select");

2:拿到选中项的索引:var index=myselect.selectedIndex ; // selectedIndex代表的是你所选中项的index

3:拿到选中项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值

以上是关于如何获得select被选中option的value和text和其他属性值的主要内容,如果未能解决你的问题,请参考以下文章

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

如何获取select 选中的option的值

JS如何控制select选中哪项?

select下拉项若是多选,如何获得其选中值?

js如何获取到select的option值???

js 获取select对象