How to set radio button status with JavaScript

前端 未结 8 1252
抹茶落季
抹茶落季 2020-12-04 21:11

What method would be best to use to selectively set a single or multiple radio button(s) to a desired setting with JavaScript?

相关标签:
8条回答
  • 2020-12-04 21:20

    Try

    myRadio.checked=true
    <input type="radio" id="myRadio">My radio<br>

    0 讨论(0)
  • 2020-12-04 21:22
    /**
     * setCheckedValueOfRadioButtonGroup
     * @param {html input type=radio} vRadioObj 
     * @param {the radiobutton with this value will be checked} vValue 
     */
    function setCheckedValueOfRadioButtonGroup(vRadioObj, vValue) {
        var radios = document.getElementsByName(vRadioObj.name);
        for (var j = 0; j < radios.length; j++) {
            if (radios[j].value == vValue) {
                radios[j].checked = true;
                break;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 21:24

    the form

    <form name="teenageMutant">
      <input value="aa" type="radio" name="ninjaTurtles"/>
      <input value="bb" type="radio" name="ninjaTurtles"/>
      <input value="cc" type="radio" name="ninjaTurtles" checked/>
    </form>
    

    value="cc" will be checked by default, if you remove the "checked" non of the boxes will be checked when the form is first loaded.

    document.teenageMutant.ninjaTurtles[0].checked=true;
    

    now value="aa" is checked. The other radio check boxes are unchecked.

    see it in action: http://jsfiddle.net/yaArr/

    You may do the same using the form id and the radio button id. Here is a form with id's.

    <form id="lizardPeople" name="teenageMutant">
      <input id="dinosaurs" value="aa" type="radio" name="ninjaTurtles"/>
      <input id="elephant" value="bb" type="radio" name="ninjaTurtles"/>
      <input id="dodoBird" value="cc" type="radio" name="ninjaTurtles" checked/>
    </form>
    

    value="cc" is checked by default.

    document.forms["lizardPeople"]["dinosaurs"].checked=true;
    

    now value="aa" with id="dinosaurs" is checked, just like before.

    See it in action: http://jsfiddle.net/jPfXS/

    0 讨论(0)
  • 2020-12-04 21:31

    I am configuring a radio button within a document fragment and tried using radiobtn.checked = true;.

    That didn't work so I instead went with this solution:

    radiobtn.setAttribute("checked", "checked");
    
    0 讨论(0)
  • 2020-12-04 21:33

    You can also explicitly set value of radio button:

    <form name="gendersForm">
      <input type="radio" name="gender" value="M" /> Man
      <input type="radio" name="gender" value="F" /> Woman
    </form>
    

    with the following script:

    document.gendersForm.gender.value="F";
    

    and corresponding radio button will be checked automatically.

    Look at the example on JSFiddle.

    0 讨论(0)
  • 2020-12-04 21:34

    Vanilla Javascript:

    yourRadioButton.checked = true;
    

    jQuery:

    $('input[name=foo]').prop('checked', true);
    

    or

    $("input:checkbox").val() == "true"
    
    0 讨论(0)
提交回复
热议问题