JQuery全部检查/取消选中所有不在ASP.NET上工作

时间:2011-07-06 03:50:08

标签: c# jquery asp.net

$(document).ready(function() {
$('#chkRFI').click(
         function() {
             $("INPUT[type='checkbox']").attr('checked', $('#chkRFI').is(':checked'));
         });   }); 


<div class="grid_3">
                <div class="box">
                    <div class="boxheader">
                        <asp:CheckBox ID="chkRFI" runat="server" Text="RFI" />
                    </div>
                    <div class="boxbody">
                        <asp:CheckBoxList ID="chklstRFI" runat="server" CssClass="boxbodylist">
                            <asp:ListItem Text="RFI No" Value="RFI" />
                            <asp:ListItem Text="RFI Date" Value="RFI_Date" />
                        </asp:CheckBoxList>
                    </div>
                </div>
            </div>

如何解决?请提供任何想法...... 感谢

4 个答案:

答案 0 :(得分:2)

您应该使用$('#<%= chkRFI.ClientID %>')代替$('#chkRFI')

答案 1 :(得分:1)

我觉得ysrb的解决方案应该可行 - 但您也可以尝试其他选择器 - 例如:

var checkAll = $('.boxheader input');
checkAll.click(function() { 
   $('.boxbody input').attr('checked', checkAll.attr('checked'));
});

答案 2 :(得分:1)

如果您使用的是ASP.NET,则所有元素ID都将以非常丑陋的形式生成,例如: $ Form1 $$ MyCheckBox(不完全是正确的样本,但它显示了主要想法)。如果您使用的是ASP.NET 4,则可以在web.config中禁用此功能([pages clientIDMode =“static”/])。使用FireBug分析您的复选框,或者只是查看页面源,以确保使用正确的ID生成复选框。希望这会有所帮助...

答案 3 :(得分:0)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Check/Uncheck All CheckBoxes Using JQuery</title>

    <script src="Scripts/jquery-1.2.6.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            $('#chkAll').click(
             function() {
                 $("INPUT[type='checkbox']").attr('checked', $('#chkAll').is(':checked'));
             });
         });    

     </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:CheckBox ID="chkAll" runat="server" Text="Check All" /><br />

        <asp:CheckBoxList ID="cbList" runat="server">
        </asp:CheckBoxList>

    </div>
    </form>
</body>
</html>

here is the complete example

相关问题