How to uncheck checked radio button

This question already has an answer here:

  • Setting “checked” for a checkbox with jQuery? 38 answers

  • This simple script allows you to uncheck an already checked radio button. Works on all javascript enabled browsers.

    var allRadios = document.getElementsByName('re');
    var booRadio;
    var x = 0;
    for(x = 0; x < allRadios.length; x++){
      allRadios[x].onclick = function() {
        if(booRadio == this){
          this.checked = false;
          booRadio = null;
        } else {
          booRadio = this;
        }
      };
    }
    <input type='radio' class='radio-button' name='re'>
    <input type='radio' class='radio-button' name='re'>
    <input type='radio' class='radio-button' name='re'>

    Radio buttons are meant to be required options... If you want them to be unchecked, use a checkbox, there is no need to complicate things and allow users to uncheck a radio button; removing the JQuery allows you to select from one of them


    You might consider adding an additional radio button to each group labeled 'none' or the like. This can create a consistent user experience without complicating the development process.

    链接地址: http://www.djcxy.com/p/73952.html

    上一篇: window.onload vs $(document).ready()

    下一篇: 如何取消选中选中的单选按钮