js/jQuery select标签 获取值/option 选中事件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/idomyway/article/details/82254125

前言

    select标签是常规下拉菜单的第一选择
    常用的select标签形式

<select id="test" name="">
    <option value="1">标签01</option>
    <option value="2">标签02</option>
</select>

一: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方法

var options=$("#test");  //获取选中的项
var value = options.val();   //拿到选中项的值
var text = options.text();   //拿到选中项的文本

三、javascript原生捕获iselect option监听事件

    在select添加onchange事件

<select name="" id=" " onchange="gradeChange()"></select>
function gradeChange(){}

四、jQuery捕获iselect option监听事件

 $(" #test").change(function(){
 });

猜你喜欢

转载自blog.csdn.net/idomyway/article/details/82254125