How can I check whether a radio button is selected with JavaScript?

前端 未结 28 2473
面向向阳花
面向向阳花 2020-11-22 00:58

I have two radio buttons within an HTML form. A dialog box appears when one of the fields is null. How can I check whether a radio button is selected?

相关标签:
28条回答
  • 2020-11-22 01:41

    I used spread operator and some to check least one element in the array passes the test.

    I share for whom concern.

    var checked = [...document.getElementsByName("gender")].some(c=>c.checked);
    console.log(checked);
    <input type="radio" name="gender" checked value="Male" />
    <input type="radio" name="gender"  value="Female" / >

    0 讨论(0)
  • 2020-11-22 01:42

    With mootools (http://mootools.net/docs/core/Element/Element)

    html:

    <input type="radio" name="radiosname" value="1" />
    <input type="radio" name="radiosname" value="2" id="radiowithval2"/>
    <input type="radio" name="radiosname" value="3" />
    

    js:

    // Check if second radio is selected (by id)
    if ($('radiowithval2').get("checked"))
    
    // Check if third radio is selected (by name and value)
    if ($$('input[name=radiosname][value=3]:checked').length == 1)
    
    
    // Check if something in radio group is choosen
    if ($$('input[name=radiosname]:checked').length > 0)
    
    
    // Set second button selected (by id)
    $("radiowithval2").set("checked", true)
    
    0 讨论(0)
  • 2020-11-22 01:44

    this is a utility function I've created to solve this problem

        //define radio buttons, each with a common 'name' and distinct 'id'. 
        //       eg- <input type="radio" name="storageGroup" id="localStorage">
        //           <input type="radio" name="storageGroup" id="sessionStorage">
        //param-sGroupName: 'name' of the group. eg- "storageGroup"
        //return: 'id' of the checked radioButton. eg- "localStorage"
        //return: can be 'undefined'- be sure to check for that
        function checkedRadioBtn(sGroupName)
        {   
            var group = document.getElementsByName(sGroupName);
    
            for ( var i = 0; i < group.length; i++) {
                if (group.item(i).checked) {
                    return group.item(i).id;
                } else if (group[0].type !== 'radio') {
                    //if you find any in the group not a radio button return null
                    return null;
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-22 01:46

    Let's pretend you have HTML like this

    <input type="radio" name="gender" id="gender_Male" value="Male" />
    <input type="radio" name="gender" id="gender_Female" value="Female" />
    

    For client-side validation, here's some Javascript to check which one is selected:

    if(document.getElementById('gender_Male').checked) {
      //Male radio button is checked
    }else if(document.getElementById('gender_Female').checked) {
      //Female radio button is checked
    }
    

    The above could be made more efficient depending on the exact nature of your markup but that should be enough to get you started.


    If you're just looking to see if any radio button is selected anywhere on the page, PrototypeJS makes it very easy.

    Here's a function that will return true if at least one radio button is selected somewhere on the page. Again, this might need to be tweaked depending on your specific HTML.

    function atLeastOneRadio() {
        return ($('input[type=radio]:checked').size() > 0);
    }
    

    For server-side validation (remember, you can't depend entirely on Javascript for validation!), it would depend on your language of choice, but you'd but checking the gender value of the request string.

    0 讨论(0)
  • 2020-11-22 01:46

    just a lil bit modification to Mark Biek ;

    HTML CODE

    <form name="frm1" action="" method="post">
      <input type="radio" name="gender" id="gender_Male" value="Male" />
      <input type="radio" name="gender" id="gender_Female" value="Female" / >
      <input type="button" value="test"  onclick="check1();"/>
    </form>
    

    and Javascript code to check if radio button is selected

    <script type="text/javascript">
        function check1() {            
            var radio_check_val = "";
            for (i = 0; i < document.getElementsByName('gender').length; i++) {
                if (document.getElementsByName('gender')[i].checked) {
                    alert("this radio button was clicked: " + document.getElementsByName('gender')[i].value);
                    radio_check_val = document.getElementsByName('gender')[i].value;        
                }        
            }
            if (radio_check_val === "")
            {
                alert("please select radio button");
            }        
        }
    </script>
    
    0 讨论(0)
  • 2020-11-22 01:47

    Note this behavior wit jQuery when getting radio input values:

    $('input[name="myRadio"]').change(function(e) { // Select the radio input group
    
        // This returns the value of the checked radio button
        // which triggered the event.
        console.log( $(this).val() ); 
    
        // but this will return the first radio button's value,
        // regardless of checked state of the radio group.
        console.log( $('input[name="myRadio"]').val() ); 
    
    });
    

    So $('input[name="myRadio"]').val() does not return the checked value of the radio input, as you might expect -- it returns the first radio button's value.

    0 讨论(0)
提交回复
热议问题