当其他复选框检查asp.net复选框时,如何取消选中该复选框

时间:2013-05-28 11:18:24

标签: jquery asp.net checkbox

我的Asp.net复选框

<asp:CheckBox ID="All" runat="server" Checked="true" Text="ALL" ForeColor="Black" /><br />
<asp:CheckBox ID="otherCheckBox2" runat="server" Checked="false" Text="Accepted" ForeColor="Black" CssClass="chkdisplay" /><br />
<asp:CheckBox ID="otherCheckBox3" runat="server" Checked="false"  Text="Contacted" ForeColor="Black" CssClass="chkdisplay"  /><br />
<asp:CheckBox ID="otherCheckBox4" runat="server" Checked="false" Text="Pending" ForeColor="Black" CssClass="chkdisplay" /><br />
<asp:CheckBox ID="otherCheckBox5" runat="server" Checked="false" Text="Pre-Authorized" ForeColor="Black" CssClass="chkdisplay" /><br />
<asp:CheckBox ID="otherCheckBox6" runat="server" Checked="false" Text="Show Deleted" ForeColor="Black" CssClass="chkdisplay" /><br />
<asp:CheckBox ID="otherCheckBox7" runat="server" Checked="false" Text="Treated" ForeColor="Black" CssClass="chkdisplay" />

我试过的脚本

 $(document).ready(function () {
            $('[id^="otherCheckBox"]').each(function () {
                if ($(this.checked)) {
                    $('#<%= All.ClientID %>').attr('checked', false);
                }
            });
        });

我想迭代asp复选框,当选中2 t0 7中的任何复选框时,取消选中id =“All”的检查。
我请求所有stackflow专家请给我一个答案,我等待很长时间的答案。 提前致谢

2 个答案:

答案 0 :(得分:2)

为什么不简单地完全消除每一个:

$('#<%= All.ClientID %>').prop('checked', !($('[id^="otherCheckBox"]').not(':checked').length));

或者如果它是固定的id:

$('#All').prop('checked', !($('[id^="otherCheckBox"]').not(':checked').length));
// class example of this same thing replaces the id prefix selector
$('#All').prop('checked', !($('.chkdisplay').not(':checked').length));

额外编辑:我怀疑你可能想要基于ALL /组进行切换,所以:你可以使用它,或者只是忽略它,如果它没有帮助你。

//if any is checked/changed reset All to reflect
$('[id^="otherCheckBox"]').on('change blur', function () {
    $('#All').prop('checked', !($('[id^="otherCheckBox"]').not(':checked').length));
});
// if All is changed, reflect the All setting (check/uncheck the group)
$('#All').on('change blur', function () {
    $('[id^="otherCheckBox"]').prop('checked', $(this).prop('checked'));
});
// set initial all state
$('[id^="otherCheckBox"]').eq(0).blur();

小提琴显示功能的最后一部分:http://jsfiddle.net/hZFFr/1/

答案 1 :(得分:1)

您的代码看起来很合理。一些指示:

  • 无需像this.checked那样用$()方式包裹All.ClientID
  • 正如您所知,通过使用$('.chkdisplay'),您的HTML复选框的ID可能与您的ASP.NET复选框的ID不完全相同。您可以考虑使用$('.chkdisplay').change或其他方法来访问感兴趣的复选框。
  • 您发布的代码只能在DOMReady上运行。您可能还想在任何.prop('checked', false)
  • 上运行它
  • 如果您运行的是jQuery 1.6或更高版本,请使用.attr而不是{{1}}