How to clear radio button in Javascript?

后端 未结 10 488
予麋鹿
予麋鹿 2020-12-02 19:57

I have a radio button named \"Choose\" with the options yes and no. If I select any one of the options and click the button labeled \"clear\", I need to clear the selected o

相关标签:
10条回答
  • 2020-12-02 20:31

    Wouldn't a better alternative be to just add a third button ("neither") that will give the same result as none selected?

    0 讨论(0)
  • 2020-12-02 20:34

    If you have a radio button with same id, then you can clear the selection

    Radio Button definition :

    <input type="radio" name="paymentType" id="paymentType" value="CASH" /> CASH
    <input type="radio" name="paymentType" id="paymentType" value="CHEQUE" /> CHEQUE
    <input type="radio" name="paymentType" id="paymentType" value="DD" /> DD
    

    Clearing all values of radio button:

    document.formName.paymentType[0].checked = false;
    document.formName.paymentType[1].checked = false;
    document.formName.paymentType[2].checked = false;
    ...
    
    0 讨论(0)
  • 2020-12-02 20:35

    In my case this got the job done:

    const chbx = document.getElementsByName("input_name");
    
    for(let i=0; i < chbx.length; i++) {
        chbx[i].checked = false;
    }
    
    0 讨论(0)
  • 2020-12-02 20:36

    if the id of the radio buttons are 'male' and 'female', value reset can be done by using jquery

    $('input[id=male]').attr('checked',false);
    $('input[id=female]').attr('checked',false);
    
    0 讨论(0)
提交回复
热议问题