开发者

How to select all values of checkbox when selectAll is selected?

开发者 https://www.devze.com 2023-02-18 22:48 出处:网络
I am trying to get all the id values of the selected checkboxes. When the user hits \"selectAll\", all the checkboxes are checked, but I just can\'t get their individual value. Any idea what I am doin

I am trying to get all the id values of the selected checkboxes. When the user hits "selectAll", all the checkboxes are checked, but I just can't get their individual value. Any idea what I am doing wrong?

Here is the jQuery:

        $(".selectAll").unbind("click").click(function(e){//needs work here
        var bool = $(this).is(":checked"); //gets whether selected or not
        var list = new Array();

        $(function(){
            $("input:checkbox").attr("checked", bool);

            if(bool == true){
            $.each((".delID :checked"),function(){
                list.push( $(this).val());
                alert( $(this).val());
            }); }
        });
    }); //END of needs work area.

and here is the html:

<form name="selectDelete" class="selectDelete">
<table id="inboxTable" class="txt13" width="800px">
<tr><th align="left" style="text-decoration:underline"><input type="checkbox" name="selectAll" class="selectAll"/></th>
    <th style="text-decoration:underline">To</th><td width="20px"></td>
    <th style="text-decoration:underline">Subject
    </th><td width="20px"></td>
    <th style="text-decoration:underline">Date</th><td width="20px"></td>
    <th style="text-decoration:underline">Action</th></tr>

<?php while($info=mysql_fetch_array($result)){
    $id = $info['id']; ?>

    echo "<tr>
            <td width="20px"><input type="checkbox" name="delID" class="delID" val开发者_StackOverflowue="'.$id.'"/></td>";

Just for anybody out there with similar problems, this is what I ended up doing to make the code work:

                if(bool == true){
            $(".delID").each(function(index,element){ 
                //list.push( $(this).val());
                alert(index + " " + $(this).val());
            }); }

In my case, if 'bool == true', then all the checkboxes will ALWAYS be checked, so that's why using just .delID, instead of .delID :checked worked for me. Thanks to all for the input.


You can use this code:

$(".delID:checked").each(function(){
    list.push( $(this).val());
    alert( $(this).val());
});

http://jsfiddle.net/ynhat/daQSM/

0

精彩评论

暂无评论...
验证码 换一张
取 消