Home  >  Article  >  Web Front-end  >  Detailed explanation of how to get the value of the select tag in js

Detailed explanation of how to get the value of the select tag in js

小云云
小云云Original
2017-12-13 14:57:127892browse

How to get the select tag value using js? In this article, we will share with you the method of obtaining the value of the select tag in js. We hope it can help you.

var obj = document.getElementByIdx_x(”testSelect”); //定位id
var index = obj.selectedIndex; // 选中索引
var text = obj.options[index].text; // 选中文本
var value = obj.options[index].value; // 选中值


Get the selected select value in jQuery

The first way

$(‘#testSelect option:selected’).text();//选中的文本
$(‘#testSelect option:selected’) .val();//选中的值
$(“#testSelect “).get(0).selectedIndex;//索引


The second way

$(“#tesetSelect”).find(“option:selected”).text();//选中的文本 
…….val(); 
…….get(0).selectedIndex;



If the select tag has an id attribute, such as
...
Use the following method to get the value of the current option:
var v = xx.value;
or
var v = document.getElementById(“xx”).value; //This method has good compatibility

If you select the tag It has a name attribute, such as


Use the following method to get the value of the current option:
var v = form1.xx.value;
or
var v = document.getElementsByName(“xx”)[0].value;
If the same page contains multiple tags with the same name attribute, the number in [0] above should be changed to the corresponding physical sequence number (counting from 0)

If the select tag does not contain any attributes that can be located, such as
...
, use the following method to obtain the value of the current option:
var v = document .getElementsByTagName(“select”)[0].value;

If the same page contains multiple select tags, the number in [0] above should be changed to the corresponding physical sequence number (Counting from 0)

For the following select tags, the method of obtaining the currently selected value is as follows:


text1
text2

code:
One: javascript native method

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;

Two: jquery method (provided that the jquery library has been loaded)

1:var options=$(“#test option:selected”);  //获取选中的项
2:alert(options.val());   //拿到选中项的值
3:alert(options.text());   //拿到选中项的文本
var obj = document.getElementByIdx_x(”testSelect”); //定位id
var index = obj.selectedIndex; // 选中索引
var text = obj.options[index].text; // 选中文本
var value = obj.options[index].value; // 选中值


Get the selected select value in jQuery

The first way

$(‘#testSelect option:selected’).text();//选中的文本
$(‘#testSelect option:selected’) .val();//选中的值
$(“#testSelect “).get(0).selectedIndex;//索引


The second way

$(“#tesetSelect”).find(“option:selected”).text();//选中的文本 
…….val(); 
…….get(0).selectedIndex;



If the select tag has an id attribute, such as
...
Use the following method to get the value of the current option:
var v = xx.value;
or
var v = document.getElementById(“xx”).value; //This method has good compatibility

If the select tag is If there is a name attribute, such as


, use the following method to obtain the value of the current option:
var v = form1.xx.value;
or
var v = document .getElementsByName(“xx”)[0].value;
If the same page contains multiple tags with the same name attribute, the number in [0] above should be changed to the corresponding physical sequence number (counting from 0)

If the select tag does not contain any attributes that can be positioned, such as
...
, use the following method to obtain the value of the current option:
var v = document. getElementsByTagName(“select”)[0].value;

If the same page contains multiple select tags, the number in [0] above should be changed to the corresponding physical sequence number ( Counting from 0)

For the following select tags, the method of obtaining the currently selected value is as follows:


text1
text2

code:
One: javascript native method

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;

Two: jquery method (provided that the jquery library has been loaded)

1:var options=$(“#test option:selected”);  //获取选中的项
2:alert(options.val());   //拿到选中项的值
3:alert(options.text());   //拿到选中项的文本


Related recommendations:

Summary of common methods for setting and obtaining Select

jQuery or Js gets the selected value or text

Analysis of the method of javascript getting the select value

The above is the detailed content of Detailed explanation of how to get the value of the select tag in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn