How to traverse radio button using js

WBOY
Release: 2016-07-25 09:12:43
Original
1563 people have browsed it

Example, js traverses radio content.

  1. 1
  2. 2
  3. 3
Copy the code

Pay attention to two things: one is how to get the value, and the other is how to traverse document.getElementById("userlist").userid; This is a method of taking the value of the name of the control element based on the id of the form. You can also use document.getElementsByName("userid") to get it directly

The difference between getElementById and getElementsByName. GetElementById can only select a single control when getting radio type elements. When getElementsByName gets radio type elements, it takes out the entire radio array. If you must use getElementById, you can first use getElementById to get the entire form like the above code. The id. Followed by the radio name

Now we know that document.getElementsByName("userid") is to get an array. The elements in the array are all the elements with the name radionum in the DOM tree. Even if there is only one radio, it is an array containing only one element. Document.all.userid is different. It gets a reference to the userid element in the page. When there are multiple radios in the page, it returns an array. If the page only contains one radio, you get this radio. Object reference. Since what you get at this time is not an array, you cannot traverse the array to make a judgment.

js function code:

  1. function getRadioBoxValue(radioName)
  2. {
  3. var obj = document.getElementsByName(radioName); //Get the control by the name of the label
  4. for(i=0; i
  5. if(obj[i].checked) {
  6. return obj[i].value;
  7. }
  8. }
  9. return "undefined";
  10. }
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!