您好,谢谢您给我的任何建议。
我有一个页面,其中有一个gridview,其中HeaderTemplate中的复选框和下面各列的复选框。所有不在HeaderTemplate中的复选框都具有“ selectCB”类。我在document.ready中创建了以下click事件,以便标题中的复选框可以用作全部检查/取消全部检查功能。选中所有复选框时,每次都会取消选中“全部选中”复选框,因此永远无法完成取消选中所有复选框的相反过程。
我已经检查了许多关于SO的帖子,这里和这里是一些示例,但到目前为止没有任何效果。该页面目前正在使用jquery 1.6.1,因此在将来进行升级之前,我只能使用这些选项。
这是点击功能:
$('#cbAll').click(function() {
event.preventDefault();
event.stopPropagation();
if ($('#cbAll').is(':checked')) {
$('#gvCheckbox').find('td:first-child input[type="checkbox"]').each(function() {
var cb = $(this);
cb.attr('checked', true);
});
$(this).attr('checked', true);
} else {
$('#gvCheckbox').find('td:first-child input[type="checkbox"]').each(function() {
var cb = $(this);
cb.attr('checked', false);
});
$(this).attr('checked', false);
}
return false;
});
这是gridview的Header和column:
<asp:TemplateField>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
<HeaderTemplate>
<input type="checkbox" id="cbAll" clientidmode="Static" />
</HeaderTemplate>
<ItemTemplate>
<input type="checkbox" id="cbOne" clientidmode="Static" CssClass="selectCB" />
</ItemTemplate>
</asp:TemplateField>
当我使用Chrome的调试器浏览此代码时,我看不到任何错误,并且直到完成处理复选框未选中的代码时,它才会出现。我尝试使用.live代替,它具有相同的结果。我还能尝试什么?还是有人可以帮忙弄清楚如何更有效地调试此问题以找到问题?
尝试change
代替click
,不要在处理程序中更改checked属性,prop
而要使用代替attr
,并为事件对象添加一个参数。我也大大简化了功能。
$('#cbAll').change(function(e) {
e.stopPropagation();
$('#gvCheckbox')
.find('td:first-child input[type="checkbox"]')
.prop('checked', this.checked);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句