1. Text box
1.1 <input type="text" name="test" id="test">
Assign the value to the variable t through var t=document.getElementById("test").value,
1.2 Of course, you can also assign known variable values to the text box in turn, for example:
var m = "5";
document.getElementById("test").value= m;
2. Drop-down list box
2.1
<select name="sel" id="sel" onchange="look();">
<option value="1" >11</option>
<option value="2" selected>22</option>
<option value="3">33</option>
</select>
The value selected in the <select> box is obtained by var s=document.getElementById("sel").value. The option of value="2" is selected by default, so the value assigned to the variable s is "2", rather than "twenty two",
If you want to assign the "value" selected in <select> such as the "text value" ("33") corresponding to "3" to the test text box, you can use the following method,
The code copy is as follows:
<script language="javascript">
function look(){
var se =document.getElementById("sel");
var option=se.getElementsByTagName("option");
var str = "" ;
for(var i=0;i<option.length;++i)
{
if(options[i].selected)
{
document.getElementById("test").value = option[i].text;
}
}
}
</script>
2.2 Compare the given value to the value in the <select> box, and select it if the value of <option> in <select> is the same as the given value.
The code copy is as follows:
var m = "2",
for(var i = 0;i<document.getElementById("sel").length;i++)
{
with(document.getElementById("sel").options[i])
{
if(value == m)
{
selected = true;
}
}
}
3. Radio box
The name attribute values of a row of radio box must be the same so that the radio selection can be achieved.
The code copy is as follows:
<INPUT TYPE="radio" NAME="a" value="1">aaaaaaaa<br>
<INPUT TYPE="radio" NAME="a" value="2">bbbbbbbbbb<br>
<INPUT TYPE="button" onclick="check();" value="test">
<script LANGUAGE="javascript">
<!--
function check()
{
var sel = 0;
for (var i = 0; i < document.getElementsByName("a").length; i++)
{
if(document.getElementsByName("a")[i].checked)
{
sel = document.getElementsByName("a")[i].value;
}
}
if(sel == 1)
{
alert("aaaaaaaaa");
}
else if(sel== 2)
{
alert("bbbbbbbbbbbb");
}
}
//-->
</script>
Js get the value and text of the selected item in the drop-down box
Get the value and text of the selected items in the drop-down box under Firefox and IE:
1. Methods supported by both IE and Firefox:
Get text
The code copy is as follows:
var obj=document.getElementById('select_template');
var text=obj.options[obj.selectedIndex].text;//Get text
var obj=document.getElementById("select_template");
for(i=0;i<obj.length;i++) {//The length of the drop-down box is its number of options
if(obj[i].selected==true) {
var text=obj[i].text;//Get text
}
}
The previous method is simpler
1.IE supports Firefox and does not support:
The code copy is as follows:
var obj=document.getElementById(name);
for(i=0;i<obj.length;i++) {
if(obj[i].selected==true) {
var text= obj[i].innerText;
}
}
Get value methods IE and Firefox are common:
var value=document.getElementById("select_template").value;//Get the value
Summary: In fact, it is mainly that both IE and Firefox support value and text attributes, and Firefox does not support innerText attributes.
Js implements the current page to open a new link:
window.location.href=url;