JS gets a simple example of the value of a text box, Drop-down box, and a radio box

Source: Internet
Author: User

  This article is mainly for JS get text box, Drop-down box, a simple example of the value of a single box to introduce, the need for friends can come to the reference, I hope to help you.

1. Text box   1.1 <input type= "text" name= "test" id= "Test" >   via var t=document.getelementbyid ("Test"). value to the variable T,   1.2 of course can also in turn to the value of known variables to the text box, 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>   through var S=document.getelementbyid (" sel "). Value gets the value selected in the <select> box, The option for value= "2" is selected by default here. So the value assigned to the variable s is "2" rather than "a",   if you want to assign the "value" ("3") corresponding to the "text value" ("33") in <select> to the Test text box, you can use the following method   Code as follows: <script language= "JavaScript" >   function Look () {        var se =document.get Elementbyid ("sel");                     var option=se.getelementsbytagname ("option");                     var str = "" ;                     for (var i=0;i<option.length;++i)  &nbs P                  {               &NB Sp    if (options[i].selected)                     { &N Bsp                  document.getelementbyid ("test"). Value = Option[i].text;                          ,       &NBS P       &NBSP {           }   </script>     2.2 will be given a value of &L The value in the T;select> box is compared, and if the value of <option> in <select> is the same as the given value, select it.   Code as follows: var m = "2",   for (var i = 0;i<document.getelementbyid ("sel"). length;i++)          {            with (documEnt.getelementbyid ("sel"). Options[i])                  {                   if (value = = m)                 &N Bsp {                 selected = true;                 &NBSP (                 }          }     3. Radio box   The Name property value of a row of a single box must be the same so that the radio can be implemented.   Code as follows: <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 selected item in the dropdown box the values and text for the dropdown box selected under Firefox and IE: the 1.IE and Firefox supported methods: &N Bsp Get text   code as follows: Var Obj=document.getelementbyid (' Select_template '); var text=obj.options[obj.selectedindex].text;//gets text   var Obj=document.getelementbyid ("Select_template");           for (i=0;i<obj.length;i++) {//dropdown box length is his option number              if (obj[i].selected==true) {              var text=obj[i].text;//get text   &NB Sp      }}     compared to the previous method is simple   1.IE support Firefox does not support:     code as follows: Var obj=document.geteleme Ntbyid (name);   for (i=0;i<obj.length;i++)  {        if (obj[i].selected==true) {    &nbsp      var text= Obj[i].innertext;              {   }     Get value method ie and Firefox general: Var Value=document.gete Lementbyid ("Select_template"). value;//Get Value   Summary: In fact, the main is IE and Firefox support the value and Text properties, Firefox does not support innertext properties.   JS to implement the current page to open a new link: window.location.href=url;  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.