JS obtains the code of the specified radio instance.

Source: Internet
Author: User
Tags radarr
JS obtains the code of the specified radio instance.

Use js to obtain the selected radio value, which is often used on the submission page. It also introduces how to obtain the radio value without selecting it.

Example,

  1. Text
  2. Script
  3. Var chk = 0;
  4. Window. onload = function (){
  5. Var chkObjs = document. getElementsByName ("radio ");
  6. For (var I = 0; I If (chkObjs [I]. checked ){
  7. Chk = I;
  8. Break;
  9. }
  10. }
  11. }
  12. Function check_radio (){
  13. Var chkObjs = document. getElementsByName ("radio ");
  14. For (var I = 0; I If (chkObjs [I]. checked ){
  15. If (chk = I ){
  16. Alert ("cannot submit if the radio value is not changed ");
  17. Break;
  18. }
  19. }
  20. }
  21. }
  22. Script
  23. Do not select to obtain the radio value

  24. USD
  25. Yen
  26. Euro

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

  1. Var USD = document. getElementsByName ("money") [0]. nextSibling. nodeValue;
  2. Var yen = document. getElementsByName ("money") [1]. nextSibling. nodeValue;
  3. Var euro = document. getElementsByName ("money") [2]. nextSibling. nodeValue;

Obtain the radio value using jQuery.

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

  2. USD

  3. Yen
  4. Euro

  5. USD

Which of the following is to be selected.

  1. Function radioValue (){

  2. Var radArr = document. getElementsByName ("radiov ");
  3. Var radValue = "";
  4. // Alert (radArr. length );
  5. For (var I = 0; I // Alert (radArr [I]. checked + "" + radArr [I]. name + "" + radArr [I]. value );
  6. If (radArr [I]. checked ){
  7. RadValue = radArr [I]. value;
  8. }
  9. }

  10. If (radValue! = Null & radValue! = ""){

  11. Alert (radValue );
  12. } Else {
  13. Alert ("Select ");
  14. }

  15. }

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.