jquery - select all checkboxes with js array name

后端 未结 4 788
长发绾君心
长发绾君心 2021-02-02 10:31

I want to use a JQuery \"check all\" function in a form like this: http://jetlogs.org/jquery/jquery_select_all.html

My problem is I am generating my form from a php scr

4条回答
  •  忘了有多久
    2021-02-02 10:51

    First of all, your name attribute should be used to set +1 elements with the same name. It's practically the same as the id attribute and should be unique per element excepted for references, but that is not the case for you.

    Try using the class attribute, it's made for the things you want to do!

    another thing is that in your code, you can only set your checkboxes to 'checked', but never to uncheck, this code will set your checkboxes to checked true or false, according to what it gets as attribute from the clicked element.

    you can do something like this:

    set your check all checkbox with an id

     
    

    Then set on all checkboxes that should be checked/unchecked a class

    
    

    And the you can do something like this in jQuery

    $("#checkAll").click( function(){
     var checkedValue = $(this).attr("checked");
     $("input.checked").attr("checked", checkedValue); });
    

    this will change all checkboxes with class checked to the same checked attribute as the one with id checkAll

提交回复
热议问题