Getting the selected radio without using “Id” but “name”

后端 未结 6 657
逝去的感伤
逝去的感伤 2020-12-19 03:17

I was trying to get selected radio button by using \"document.getElementByName(\'nameOfradio\')\" because all of the radio buttons share the same name. But, nothing happened

相关标签:
6条回答
  • 2020-12-19 03:22

    This is exactly why you should use a javascript library.

    document.querySelector('input[name=nameOfradio]');
    

    for example is not supported before IE8.

    Let the library handle the browser craziness.

    In jQuery you can just use $('input[name=radioName]:checked').val() or $("form").serialize() and be done with it.

    0 讨论(0)
  • 2020-12-19 03:26

    Save yourself some pain in the later js dev and use a js library like jQuery. Then you can do something like $('input[name=radioName]:checked').val()

    0 讨论(0)
  • 2020-12-19 03:26

    You can use the class property if your looking for a quick solution. Many elements can have the same class. use the command:

    document.getElementsByClass('nameOfradio');
    

    In addition you should use the correct form of getting elements by name which is:

    document.getElementsByName('nameOfradio');
    

    You can also use the following code to find the selected radio value as follows:

    radioObj=document.getElementsById('nameOfradio');
    var radioLength = radioObj.length;
    if(radioLength == undefined) {
        if(radioObj.checked) {
            return radioObj.value;
        } else {
            return "";
        }
    }
    for(var i = 0; i < radioLength; i++) {
        if(radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
    
    0 讨论(0)
  • 2020-12-19 03:27
    document.querySelector('input[name=nameOfRadio]:checked').value
    

    Eg:-

    <form>
      <input type="radio" name="gender" value="male"> Male<br>
      <input type="radio" name="gender" value="female"> Female<br>
      <input type="radio" name="gender" value="other"> Other  
    </form> 
    
    document.querySelector('input[name=gender]:checked').value
    

    Also, you can add a checked attribute to a default radio button among the group of radio buttons if needed

    <input type="radio" name="gender" value="male" checked> Male<br>
    <input type="radio" name="gender" value="female"> Female<br>
    <input type="radio" name="gender" value="other"> Other
    
    0 讨论(0)
  • 2020-12-19 03:32

    This line:

    document.getElementByName('nameOfradio').value
    

    should be:

     document.querySelector('input[name=nameOfradio]:checked').value;
    

    using querySelector

    Note that CSS pseudo-classes are accessed by a colon (:).

    0 讨论(0)
  • 2020-12-19 03:39

    Demo: http://jsfiddle.net/majidf/LhDXG/

    Markup

    Sex:<br/> 
    <input type="radio" name="gender" value="male" /> Male<br/>
    <input type="radio" name="gender" value="female" /> Female
    <br/>
    Age group:<br/> 
    <input type="radio" name="ageGroup" value="0-3" /> 0 to 3<br/>
    <input type="radio" name="ageGroup" value="3-5" /> 3 to 5<br/>
    <input type="radio" name="ageGroup" value="5-10" /> 5 to 10<br/>
    
    <button onclick="getValues();">Get values</button>
    

    JS

    function getRVBN(rName) {
        var radioButtons = document.getElementsByName(rName);
        for (var i = 0; i < radioButtons.length; i++) {
            if (radioButtons[i].checked) return radioButtons[i].value;
        }
        return '';
    }
    
    function getValues() {
        var g = getRVBN('gender');
        var a = getRVBN('ageGroup');
        alert(g + ' ' + a);
    }
    
    0 讨论(0)
提交回复
热议问题