简体   繁体   中英

Select all textboxes using jQuery

I have code which displayed a person's info in a table(fields:name, surname, address, etc.) and one of the inputs is a checkbox. The code is as follows:

  $("#table").append('<tr class="trow'+j+'">'+
                      '<td class="ids" id="z'+i+'">'+totrecs+'</td>'+
              '<td>'+member[i].jdate+'</td>'+
              '<td class="users" 

              '<td id="contact'+i+'">'+member[i].fname+' '+member[i].lname+'</td>'+
              '<td id="myaddress'+i+'">'+member[i].address1+' '+member[i].town+'</td>'+

              '<td><input type="checkbox" name="whome" id="showMe'+i+'"'+
                                             'class="boxes" onclick="getMe('+i+')" /></td></tr>');  
      totrecs++;
      j++;
     }

What I am tryin to do is program a function that when clicking a certain button all of the checkboxes will be selected/checked.

I would appreciate any help. Thank You.

<input type="button" value="Check/Uncheck All" />

$('#btnId').on('click', function() {

    var check = false;

    if( !$(this).hasClass('checked')){
        bool = true;
    }

    $(this).toggleClass('checked');

    $('#table input[type="checkbox"]').prop('checked', check)
});

这可能会有所帮助: JQquery 选择所有复选框教程

To check all checkboxes in #table using jQuery:

$('#table :checkbox').attr('checked', '');

Note that this will not trigger any click events you set for the checkboxes. You should use the change event instead of click to make it happen as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM