How to get value of selected radio button?

前端 未结 28 2263
温柔的废话
温柔的废话 2020-11-22 03:51

I want to get the selected value from a group of radio buttons.

Here\'s my HTML:

相关标签:
28条回答
  • 2020-11-22 04:39

    directly calling a radio button many times gives you the value of the FIRST button, not the CHECKED button. instead of looping thru radio buttons to see which one is checked, i prefer to call an onclick javascript function that sets a variable that can later be retrieved at will.

    <input type="radio" onclick="handleClick(this)" name="reportContent" id="reportContent" value="/reportFleet.php" >
    

    which calls:

    var currentValue = 0;
    function handleClick(myRadio) {
        currentValue = myRadio.value;
        document.getElementById("buttonSubmit").disabled = false; 
    }
    

    additional advantage being that i can treat data and/or react to the checking of a button (in this case, enabling SUBMIT button).

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

    Another (apparently older) option is to use the format: "document.nameOfTheForm.nameOfTheInput.value;" e.g. document.mainForm.rads.value;

    document.mainForm.onclick = function(){
        var radVal = document.mainForm.rads.value;
        result.innerHTML = 'You selected: '+radVal;
    }
    <form id="mainForm" name="mainForm">
        <input type="radio" name="rads" value="1" />
        <input type="radio" name="rads" value="2" />
        <input type="radio" name="rads" value="3" />
        <input type="radio" name="rads" value="4" />
    </form>
    <span id="result"></span>

    You can refer to the element by its name within a form. Your original HTML does not contain a form element though.

    Fiddle here (works in Chrome and Firefox): https://jsfiddle.net/Josh_Shields/23kg3tf4/1/

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

    An improvement to the previous suggested functions:

    function getRadioValue(groupName) {
        var _result;
        try {
            var o_radio_group = document.getElementsByName(groupName);
            for (var a = 0; a < o_radio_group.length; a++) {
                if (o_radio_group[a].checked) {
                    _result = o_radio_group[a].value;
                    break;
                }
            }
        } catch (e) { }
        return _result;
    }
    
    0 讨论(0)
  • 2020-11-22 04:42

    If the buttons are in a form
    var myform = new FormData(getformbywhatever); myform.get("rate");
    QuerySelector above is a better solution. However, this method is easy to understand, especially if you don't have a clue about CSS. Plus, input fields are quite likely to be in a form anyway.
    Didn't check, there are other similar solutions, sorry for the repetition

    0 讨论(0)
  • 2020-11-22 04:43
    <form id="rates">
      <input type="radio" name="rate" value="Fixed Rate"> Fixed
      <input type="radio" name="rate" value="Variable Rate"> Variable
      <input type="radio" name="rate" value="Multi Rate" checked> Multi
    </form>
    

    then...

    var rate_value = rates.rate.value;
    
    0 讨论(0)
  • 2020-11-22 04:47

    Shortest

    [...rates.children].find(c=>c.checked).value
    

    let v= [...rates.children].find(c=>c.checked).value
    
    console.log(v);
    <div id="rates">
      <input type="radio" id="r1" name="rate" value="Fixed Rate"> Fixed Rate
      <input type="radio" id="r2" name="rate" value="Variable Rate"> Variable Rate
      <input type="radio" id="r3" name="rate" value="Multi Rate" checked="checked"> Multi Rate  
    </div>

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