问题描述
我有以下ASP.NET服务器控件复选框.
I have following ASP.NET server control checkboxes.
<asp:checkbox id="chkMonS1" runat="server" />
<asp:checkbox id="chkMonS2" runat="server" />
<asp:checkbox id="chkAllMon" runat="server" />
还有其他复选框.
<asp:checkbox id="chkTueS1" runat="server" />
<asp:checkbox id="chkTueS2" runat="server" />
<asp:checkbox id="chkAllTue" runat="server" />
因此,我尝试使用jQuery来选择所有复选框,如..
So I tried to use jQuery to select all check boxes like so..
<script type="text/javascript">
$('chkAllMon').click(
function () {
$('chkMonS1').attr('checked', true)
$('chkMonS2').attr('checked', true)
}
)
</script>
但是,它不起作用.哪里出问题了?
However, it doesn't work. Where did it go wrong?
推荐答案
如果您使用的是ASP.NET 4.0或更高版本,则很容易解决.
If you are using ASP.NET 4.0 or newer, this is easy to fix.
首先,在jQuery选择器中需要一个#
符号,如下所示:$('#chkMonS1')
.
First, you need a #
symbol in your jQuery selectors, like this: $('#chkMonS1')
.
此外,您需要在ASP.NET元素中设置ClientIdMode="Static"
,如下所示.这会将HTML id
设置为与ASP.NET id
相同的值.请参阅本文以获取有关原因.
Also, you'll need to set ClientIdMode="Static"
in the ASP.NET elements, as shown below. This will set the HTML id
to the same value as the ASP.NET id
. See this article for the reason why.
<asp:checkbox id="chkAllTue" runat="server" ClientIDMode="Static" />
这篇关于使用jQuery检查ASP.NET复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!