【问题标题】:How to select all values of checkbox when selectAll is selected?选择全选时如何选择复选框的所有值?
【发布时间】:2011-03-23 14:20:18
【问题描述】:

我正在尝试获取所选复选框的所有 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 对我有用。 感谢大家的投入。

【问题讨论】:

    标签: php javascript jquery


    【解决方案1】:

    您可以使用此代码:

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

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

    【讨论】:

    • 是的,我最终自己想通了。感谢您的热心意见。
    猜你喜欢
    • 2012-05-08
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多