如果选中了CheckAll,则选择all,如果没有,则使用jQuery从Gridview中取消全部

时间:2013-04-22 15:08:30

标签: c# jquery gridview checkbox hiddenfield

我只是坚持使用复选框操作,我有一个Gridview第一列作为标题和行中的复选框,复选框与数据库中的iID值绑定。我想删除行有时“全部”(按标题复选框),有时“已选中”(通过复选框)。现在我正在使用这段代码:

的JavaScript

function checkcheckboxPrint() {
            var checkfag = false;
            var id = 0;
            var table = document.getElementById('<%=grdRefPhysician.ClientID%>');

            if (table != null) {
                for (i = 1; i < table.rows.length; i++) {
                    var chkRead = table.rows[i].cells[0].getElementsByTagName("input")[0];
                    var DivID = table.rows[i].cells[0].getElementsByTagName('div')[0];

                    if (chkRead != null) {
                        if (chkRead.type == 'checkbox') {
                            if (chkRead.checked) {
                                id = DivID.innerHTML + "," + id;
                                checkfag = true;
                                alert(id);
                            }
                        }
                    }
                }
                if (checkfag == true) {
                    $("#<%=hdnIDs.ClientID %>").val(id);
                    return true;
                }
                else {
                    alert("Please select record to delete.");
                    return false;
                }
            }
        }

的jQuery

    $(document).ready(function () {
        $("input:checkbox").change(function () {
            $("#DivDelete").show('slow');
        });
    });

正如您所看到的,我正在使用隐藏字段将这些逗号(,)分隔的ID提供给服务器端,基本上它的工作原理但是,我不会通过一个场景,如果我检查标题行然后应该选中所有复选框,如果我取消选中它,那么应该取消选择所有复选框,就像反之,我点击这个调用这个js方法

HTML

 <div id="DivDelete" style="text-align:left;display:none;">
        <asp:Button ID="btnDeleteAll" OnClientClick="return checkcheckboxPrint();" runat="server" Text="Delete" />
 </div>

我只想使用jQuery将逗号分隔的ID存储到隐藏字段,并使用checkAll和独立的检查工具!

我网格的附加屏幕截图

enter image description here

任何帮助表示赞赏! 感谢

1 个答案:

答案 0 :(得分:0)

我不认为

table.rows[i].cells[0]

将用于js对象。你需要做的是为每个复选框添加一个更改处理程序,更新一个要删除的所选id的数组(你可以在当前复选框上使用.parent()。find(yourIdDiv)找到id。检查长度用户单击DeleteAll并依次继续运行时的数组。