Javascript or Jquery to check and uncheck all checkbox

前端 未结 6 2069
忘了有多久
忘了有多久 2020-12-21 04:41

I have a form with a list of checkboxs. I want to create a checkAll and UncheckAll boxes for better user experience. I tried a lot of code that I got from Internet, but none

6条回答
  •  星月不相逢
    2020-12-21 05:33

    var form = document.getElementById('groupImportForm');
    var inputs = form.elements;
    var checkboxes = [];
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == 'checkbox') {
            inputs[i].checked =true;
        }
    }
    

    Javascript version of the example

    Same thin using jQuery

    $(".uncheckAll']").click(function() {
       $('.inputs').attr('checked','checked'); //add the atrribute checked
       // $('.inputs').removeAttr("checked");     //remove the attribute checked
    });
    

    Working example of jQuery

提交回复
热议问题