How to implement “select all” check box in HTML?

前端 未结 29 2897
悲哀的现实
悲哀的现实 2020-11-22 11:09

I have an HTML page with multiple checkboxes.

I need one more checkbox by the name \"select all\". When I select this checkbox all checkboxes in the HTML page must b

29条回答
  •  深忆病人
    2020-11-22 11:45

    If adopting the top answer for jquery, remember that the object passed to the click function is an EventHandler, not the original checkbox object. Therefore code shoudl be modified as follows.

    Html

     Toggle All
    Bar 1
    Bar 2
    Bar 3
    Bar 4

    Javascript

    $(function() {
        jQuery("[name=selectThemAll]").click(function(source) { 
            checkboxes = jQuery("[name=foo]");
            for(var i in checkboxes){
                checkboxes[i].checked = source.target.checked;
            }
        });
    })
    

提交回复
热议问题