欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js如何通过类名 获得selece下拉框的值

程序员文章站 2022-04-10 19:37:51
通过ID获得下拉框的值:1 用户类型:2 1 var myType = docum....
通过ID获得下拉框的值:
1  用户类型:
2  <select name="type" id="userType">
3     <option value="0">请选择</option>
4     <option value="1">普通类型</option>
5     <option value="2">VIP类型</option>
6</select>
1 var myType = document.getElementById("userType");//获取select对象
2 var index = myType.selectedIndex; //获取选项中的索引,selectIndex表示的是当前所选中的index
3 myType.options[index].value;//获取选项中options的value值
4 myType.options[index].text;//获取选项中options的value值

 

通过类名获得下拉框的值:

var value1_obj= document.getElementsByClassName("value1");//获取select对象
var index = value1_obj[0].selectedIndex; //获取选项中的索引,selectIndex表示的是当前所选中的index
value1_obj[0].options[index].value;//获取选项中options的value值
value1_obj[0].options[index].text;//获取选项中options的value值

通过标签名称获得下拉框的值:

var value = document.myForm.mySelect.value;

//myForm是表单form的name

//mySelect是下拉框selece的name

本文地址:https://blog.csdn.net/fghfghhgjgj/article/details/109261035

相关标签: js 1024程序员节