Home > Web Front-end > JS Tutorial > js code to get server control value_javascript skills

js code to get server control value_javascript skills

WBOY
Release: 2016-05-16 18:33:02
Original
1017 people have browsed it
1. Get the value and text of the selected item in the drop-down list (select)
The code select.htm example is as follows:
Copy code The code is as follows:


Get the value and text of the selected item in the drop-down list (select)


<script> <br>//Get the text of the selected item in the drop-down list<br>function getSelectedText(name){ <br>var obj=document.getElementById( name); <br>for(i=0;i<obj.length;i ){ <BR>if(obj[i].selected==true){ <BR>return obj[i].innerText; // The key is to get the option text through the innerText property of the option object <BR>} <BR>} <BR>} <BR>//Get the value of the selected item in the drop-down list <BR>function getSelectedValue(name){ <BR>var obj =document.getElementById(name); <BR>return obj.value; //So simple, you can get it directly by using the value attribute of the object<BR>} <BR></script>






2. Get the radio button (radio) group Value and modify the selected item
I saw many posts saying that js can directly use document.getElementById("oper").value to get the value of the radio button group, although it is the same as the radio button group. The drop-down list (also an array of list items) is also an array. In this way, you can get the value of the drop-down list, but the radio button group cannot get the selected value. After careful study, the summary is as follows:
Unlike the drop-down list, the radio button must use this.form.oper or document.getElementsByName('oper') to obtain the array object. document.getElementById('oper') cannot Get the array object (select can). Moreover, the value must be obtained through a loop judgment, and .value cannot be used directly (select can be used). To change the selected item of the radio button group, you must also use loop judgment to change the value of each radio button.
The test code radio.html is as follows:
Copy the code The code is as follows:



Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template