jQuery: make checkboxes act like radio buttons?

后端 未结 11 1614
栀梦
栀梦 2020-11-28 10:29

Is there a way to make checkboxes act like radio buttons? I assume this could be done with jQuery?



        
11条回答
  •  渐次进展
    2020-11-28 11:18

    Updated the script (via answer from Tomislav) so you also can uncheck ALL checkboxes. Just added a .not(this) and removed the line where the the current checkbox is checked.

    $('form').each(function() { // iterate forms
      var $this = $(this);
      $this.find('input:checkbox').click(function() {
        var group = 'input:checkbox[name="' + $(this).attr('name') + '"]';
        $this.find(group).not(this).attr('checked', false).prop('checked', false); // also use prop, for real Property change
      });
    });
    

    http://jsfiddle.net/kya0639w/

提交回复
热议问题