问题描述
我在寻找以下 jquery/checkbox 行为的原因时遇到问题.
I have a problem finding reason for the following jquery/checkbox behaviour.
$( this.obj + ' table.sgrid-content > thead > tr > th > input.select_all' ).on( 'click' , {grid:this} , function(event){
var grid = event.data.grid;
if( $(this).is(':checked') ){
$( grid.obj + ' table.sgrid-content > tbody > tr > td > input.select ' ).attr('checked','checked');
$( grid.obj + ' .sgrid-content > tbody > tr > td > input.select ' ).parents('tr').addClass('ui-state-highlight');
} else {
$( grid.obj + ' table.sgrid-content > tbody > tr > td > input.select ' ).removeAttr('checked');
$( grid.obj + ' table.sgrid-content > tbody > tr > td > input.select ' ).parents('tr').removeClass('ui-state-highlight');
}
});
代码的工作原理如下:- 点击 input.select_all 触发事件- 如果 input.select_all 被选中:将选中的属性添加到 table.sgrid-content 中标记为 .select 的所有复选框- 如果不是:从所有 input.select 项目中删除 'checked' 属性.
The code is intended to work as follows: - click on input.select_all triggers the event - if input.select_all is checked: add attribute checked to all checkboxes marked as .select within table.sgrid-content - if not: remove the 'checked' attribute from all input.select items.
另一个简单的网格函数.它有效.奇怪的是,它只工作一次.我的意思是,您可以选择所有复选框并取消选择它们.在该操作之后,全选"功能停止工作.
Yet another simple grid function. And it works. The weird part is, it works only once. By that I mean, you can select all the checkboxes and deselect them. After that operation "Select all" function stops working.
另一件奇怪的事情是,当我用 firebug 检查 dom 元素时,它们都按照应有的方式变成了 checked='checked' attr,但它们的显示和行为与未检查时一样.
Another weird thing is, when i check dom elements with firebug they all become checked='checked' attr as they should, but they display and behave as they were not checked.
选择器正常工作.添加/删除 ui-state-highlight 的代码部分一直有效.
Selectors work as they should. The code part with adding/removing ui-state-highlight works all the time.
解释词:grid - 是我传递给 grid.obj 的对象(基本上是某个 div 的 ID)
Word of explenation:grid - is the object that I pass to get grid.obj ( basically ID of a ceratain div )
请给我你的意见.
推荐答案
使用 prop('checked', true/false) 代替 removeAttr
Use prop('checked', true / false) instead of removeAttr
$('input[name=foo]').prop('checked', true);
$('input[name=foo]').prop('checked', false);
这篇关于jquery attr('checked','checked') 只工作一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!