Home > Backend Development > PHP Tutorial > JS gets radio selected value example code

JS gets radio selected value example code

WBOY
Release: 2016-07-25 09:12:41
Original
1406 people have browsed it

Use js to get the selected value of the radio, which is often used on the submission page. At the same time, it also introduces how to get the value of the radio without selecting it.

Example,

  1. <script> </li> <li>var chk = 0; </li> <li>window.onload=function (){ </li> <li>var chkObjs = document.getElementsByName("radio"); </li> <li>for(var i=0;i<chkObjs .length;i++){ <li>if(chkObjs[i].checked){ <li>chk = i; <li>break; <li>} <li>} <li>} <li>function check_radio(){ <li>var chkObjs = document.getElementsByName("radio"); <li>for(var i=0;i<chkObjs.length;i++){ <li>if(chkObjs[i].checked){ <li>if(chk == i){ <li>alert("The radio value has not changed and cannot be submitted"); <li>break; <li>} <li>} <li>} <li>} <li></script>
  2. 一;
  3. two;
  4. three;
  5. four;
  6. 五;

  7. Get the value of radio without making a choice

  8. JPY
  9. Euro< ;/p>
Copy code

Native JS method: (Native DOM operations will treat text as a node, so there will be nextSibling)

  1. var dollar = document.getElementsByName("money")[0].nextSibling.nodeValue;
  2. var yen = document.getElementsByName("money")[1].nextSibling.nodeValue;
  3. var euro= document.getElementsByName("money")[2].nextSibling.nodeValue;
Copy code

jQuery method to get the radio value.

  1. $('input[name="money"]:checked').next('span').html();

  2. < input type="radio" name="money" value="1" checked="checked" />USD

  3. Japanese yen
  4. Euro< ;/p>
Copy code

The following are to be selected , only to determine whether there is a choice.

  1. function radioValue(){

  2. var radArr = document.getElementsByName("radiov");
  3. var radValue = "";
  4. //alert(radArr.length);
  5. for(var i=0; i
  6. //alert(radArr[i].checked+" "+radArr[i].name + " "+ radArr[i].value);
  7. if(radArr[ i].checked){
  8. radValue = radArr[i].value;
  9. }
  10. }

  11. if(radValue != null && radValue != ""){

  12. alert(radValue);
  13. }else{
  14. alert("Please select");
  15. }

  16. }

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