簡體   English   中英

選擇selectAll后如何選擇復選框的所有值?

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

我正在嘗試獲取所選復選框的所有id值。 當用戶點擊“ selectAll”時,所有復選框均被選中,但是我只是無法獲得他們各自的值。 知道我在做什么錯嗎?

這是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.

這是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" value="'.$id.'"/></td>";

對於那里遇到類似問題的任何人,這就是我最終使代碼正常工作的目的:

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

在我的情況下,如果'bool == true',則將始終選中所有復選框,因此這就是為什么只使用.delID而不是.delID的原因:checked對我有用。 感謝大家的投入。

您可以使用以下代碼:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM