JS、JQUERY获取select被选中option的value和text的值 您所在的位置:网站首页 jquery获取id的值 JS、JQUERY获取select被选中option的value和text的值

JS、JQUERY获取select被选中option的value和text的值

#JS、JQUERY获取select被选中option的value和text的值 | 来源: 网络整理| 查看: 265

在做网站时,在select选择框中,往往需要获取select被选中option的value和text的值,并且传给其它位置使用。获取select被选中option的value和text的值,一般需要使用JS或者JQUERY来获取。

JS、JQUERY获取select被选中option的value和text的值

学做网站论坛 WP模板阁

JS获取select被选中option的value和text的值

1:拿到select对象: var myselect=document.getElementById("select"); 2:拿到选中项的索引: var index=myselect.selectedIndex;

// selectedIndex代表的是你所选中项的index

3:拿到选中项options的value: myselect.options[index].value;

或者直接使用:

myselect.options[myselect.selectedIndex].value; 4:拿到选中项options的text: myselect.options[index].text;

或者直接使用:

myselect.options[myselect.selectedIndex].text; 5:拿到选中项的其他值,比如这里的url: myselect.options[index].getAttribute('url'); 或者直接使用:[cce_js]myselect.options[myselect.selectedIndex].getAttribute('url');

JQUERY获取select被选中option的value和text的值

1: var options=$(“#select option:selected”);

//获取选中的项

2: alert(options.val());

//拿到选中项的值

3: alert(options.text());

//拿到选中项的文本

4: alert(options.attr('url'));

//拿到选中项的url值



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有