JavaScript to get radio selected value _ form effects

Source: Internet
Author: User

The principle is: the general use of traversal method to determine whether each radio is selected, if it is, and then take its value.

<form id= "userlist" method= "post" action= "option.php" >
<input type= "Radio" name= "userid" value= "1" >1
<input type= "Radio" name= "userid" value= "2" >2 "<input" type= "Radio"
name= "userid" value= "3" >3
</form>
<script language= "javascript" >
function Usubmit (action) {
var radionum = document.getElementById ("UserList"). UserID;
for (Var i=0;i<radionum.length;i++) {
if (radionum[i].checked) {
UserID = Radionum[i].value
}
}
window.location.href= ' option.php?action= ' +action+ ' &userid= ' +userid;
}
</script>

Note that the above needs to be aware that the ID (userlist) is set in the form

Here are two places to note: How to take a value, how to traverse
document.getElementById ("UserList"). UserID;
This is the method that takes the name of the control element to the value based on the ID of the form.
can also be obtained directly with Document.getelementsbyname ("userid")

The difference between getElementById and Getelementsbyname, getElementById take radio type elements can only select a single control, Getelementsbyname Radio type elements are taken out of the entire radio array, if you must use getElementById, You can use getElementById to get the ID of the entire form, just like the code above. Immediately followed by the radio name

Now you know that Document.getelementsbyname ("userid") is an array of elements in the DOM tree that have all the name radionum, even if there is only one radio, and an array containing only one element.
And Document.all.userid is different, it is to get the UserID element in the page reference, when there are multiple radio in the page, it returns an array, if the page contains only one radio, then get the Radio object reference. Because this is not an array, it is not possible to traverse the array for judgment.

into functions, as follows:

function  Getradioboxvalue (radioname) 
{ 
      var obj = document.getelementsbyname (radioname); This is the name of the label to fetch the control for
         (i=0 i<obj.length;i++)  {

         if (obj[i].checked)  {return  obj[i]. value; 
         } 
       }     
       return "undefined";    
}

JS Gets the value selected in radio

 function Foo () {var selectedindex =-1;
  var Form1 = document.getElementById ("Form1");
  
  var i = 0;
      For (i=0 i<form1.r.length; i++) {if (form1.r[i].checked) {selectedindex = i;
      Alert ("The value of your selected item is:" + form1.r[i].value);
    Break
  } if (SelectedIndex < 0) {alert ("You did not select any item"); }
}

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.