How to get the selected radio button’s value?

后端 未结 18 1570
北海茫月
北海茫月 2020-11-22 00:57

I’m having some strange problem with my JS program. I had this working properly but for some reason it’s no longer working. I just want to find the value of the radio button

相关标签:
18条回答
  • 2020-11-22 01:05

    Since jQuery 1.8, the correct syntax for the query is

    $('input[name="genderS"]:checked').val();
    

    Not $('input[@name="genderS"]:checked').val(); anymore, which was working in jQuery 1.7 (with the @).

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

    This is pure JavaScript, based on the answer by @Fontas but with safety code to return an empty string (and avoid a TypeError) if there isn't a selected radio button:

    var genderSRadio = document.querySelector("input[name=genderS]:checked");
    var genderSValue = genderSRadio ? genderSRadio.value : "";
    

    The code breaks down like this:

    • Line 1: get a reference to the control that (a) is an <input> type, (b) has a name attribute of genderS, and (c) is checked.
    • Line 2: If there is such a control, return its value. If there isn't, return an empty string. The genderSRadio variable is truthy if Line 1 finds the control and null/falsey if it doesn't.

    For JQuery, use @jbabey's answer, and note that if there isn't a selected radio button it will return undefined.

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

    I like to use brackets to get value from input, its way more clear than using dots.

    document.forms['form_name']['input_name'].value;
    
    0 讨论(0)
  • 2020-11-22 01:07

    If it is possible for you to assign a Id for your form element(), this way can be considered as a safe alternative way (specially when radio group element name is not unique in document):

    function findSelection(field) {
        var formInputElements = document.getElementById("yourFormId").getElementsByTagName("input");
        alert(formInputElements);
            for (i=0; i < formInputElements.length; i++) {
            if ((formInputElements[i].type == "radio") && (formInputElements[i].name == field) && (formInputElements[i].checked)) {
                alert(formInputElements[i].value + ' you got a value');     
                return formInputElements[i].value;
            }
        }
    }
    

    HTML:

    <form action="#n" name="theForm" id="yourFormId">
    
    0 讨论(0)
  • 2020-11-22 01:09

    You can do something like this:

    var radios = document.getElementsByName('genderS');
    
    for (var i = 0, length = radios.length; i < length; i++) {
      if (radios[i].checked) {
        // do whatever you want with the checked radio
        alert(radios[i].value);
    
        // only one radio can be logically checked, don't check the rest
        break;
      }
    }
    <label for="gender">Gender: </label>
    <input type="radio" name="genderS" value="1" checked="checked">Male</input>
    <input type="radio" name="genderS" value="0">Female</input>

    jsfiddle

    Edit: Thanks HATCHA and jpsetung for your edit suggestions.

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

    This works with any explorer.

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

    This is a simple way to get the value of any input type. You also do not need to include jQuery path.

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