JS gets a simple instance of the value of a text box a drop down box or a single box

  • 2020-03-30 02:09:50
  • OfStack

1. The text box

1.1 < Input type = "text" name = "test" id = "test" >

Var t= document.getelementbyid ("test").value,

1.2 of course, you can also assign the known values of variables to the text box in reverse, for example:

Var m = "5";
Document. The getElementById (" test "). The value = m;

2. Drop-down list box

2.1
< Select name = "sel" id = "sel" onchange = "look ();" >
< The option value = "1" > 11 < / option>
< The option value = "2" selected> 22 < / option>
< The option value = "3" > 33 < / option>
< / select>

Var s= document.getelementbyid ("sel"). Value < Select> Select the value in the box. By default, value="2" is selected, so the value assigned to variable s is "2" instead of "22".

If you want to put < Select> The selected "value" such as the corresponding "text value "("33") of "3" in", can be assigned to the test text box by the following method:


<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 match the given value with < Select> The values in the box are compared if < Select> The < Option> Is the same as the given value, then it is selected.

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. The radio buttons

The name attribute of a row of marquee boxes must have the same value to enable radio selection.


<INPUT TYPE="radio" NAME="a" value="1">aaaaaaaaaa<br>
<INPUT TYPE="radio" NAME="a" value="2">bbbbbbbbb<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("aaaaaaaaaa");
      }
   else if(sel== 2)
   {
       alert("bbbbbbbbb");
    }
}
//-->
</script>

Js gets the value and text of the drop-down box selected item

Get the value and text of the selected item in the drop-down box under Firefox and IE:

Get the text


var obj=document.getElementById('select_template');
var text=obj.options[obj.selectedIndex].text;//Get the text
var obj=document.getElementById("select_template");
        for(i=0;i<obj.length;i++) {//The length of the drop-down box is the number of options he has
           if(obj[i].selected==true) {
            var text=obj[i].text;//Get the text
        }
}

Both methods are more concise than the previous one

Js implementation of the current page opens a new link:
Window. The location. Href = url;


Related articles: