简体   繁体   中英

How to select all checkbox and get value in jquery

In foreach loop there is checkbox for each row.

Code As Bellow.

foreach($rpd_records as $rpd_newslater_records)
{
    $rp_ne_value = maybe_unserialize($rpd_newslater_records->meta_value); ?>
    <tr>
        <input type="hidden" class="rpd_meta_id" name="rpd_meta_id" value="<?php echo $rp_ne_records->meta_id; ?>">
        <td><input type="checkbox"></td>
        <td><?php echo $rp_ne_value['product_id']; ?></td>
        <td> <div class="send_mail_btn" style="display: inline;">
        <a href="javascript:void(0)" class="rpd_send_it">Send</a></div></td>
    </tr>
    <?php
} ?>

<button type="button" id="sendAll" class="main"><span class="sub"></span> Send All </button>    

What I should : when i click on SendAll Button then its all checkbox are selected and get each of Row Hidden Value using Jquery.

Can you suggest me.

Thanks.

You can

1) traverse to closest tr element

2) find hidden input in it.

3) use .map() and get jquery object of all hidden input values

4) convert to array using .get()

 $('#sendAll').click(function(){
  var inputcheckboxes = $('input:checked').attr("checked", true);    
  var hiddenValForChecked = inputcheckboxes.find('.rpd_meta_id').map(function(){
    return this.value;
  }).get();
  console.log(hiddenValForChecked);
 });

Here hiddenValForChecked represents array of values of hidden fields.

This will help you;

$("#sendAll").click(function(e) {
    $(":checkbox").attr("checked", true);    

    var values = $('input.rpd_meta_id[type="hidden"]').map(function(){
        return this.value;
    }).get();

    alert(values);
});

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