jquery attr('checked','checked') 只工作一次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15266533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery attr('checked','checked') works only once
提问by PiWo
I have a problem finding reason for the following jquery/checkbox behaviour.
我在寻找以下 jquery/复选框行为的原因时遇到问题。
$( 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');
}
});
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.
该代码旨在按如下方式工作: - 单击 input.select_all 触发事件 - 如果选中 input.select_all:将选中的属性添加到 table.sgrid-content 中标记为 .select 的所有复选框 - 如果不是:删除“已选中” ' 属性来自所有 input.select 项目。
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.
另一个简单的网格函数。它有效。奇怪的是,它只工作一次。我的意思是,您可以选择所有复选框并取消选择它们。在该操作之后,“全选”功能停止工作。
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.
另一个奇怪的事情是,当我用 firebug 检查 dom 元素时,它们都按照应有的方式变成了 checked='checked' attr,但它们的显示和行为与未检查时一样。
Selectors work as they should. The code part with adding/removing ui-state-highlightworks all the time.
选择器按其应有的方式工作。添加/删除ui-state-highlight的代码部分一直有效。
Word of explenation: grid- is the object that I pass to get grid.obj ( basically ID of a ceratain div )
说明词: grid- 是我传递给 grid.obj 的对象(基本上是某个 div 的 ID)
Please give me your opinion.
请给我你的意见。
回答by Marek Musielak
Use prop('checked', true / false)instead of removeAttr
使用prop('checked', true / false)而不是 removeAttr
$('input[name=foo]').prop('checked', true);
$('input[name=foo]').prop('checked', false);
回答by Suraj
You can change the attribute, and that will also change the property, if the element is untouched. Once the element leaves this initial state, changing the attribute no longer affects the property. The exact behavior probably varies between browsers.
您可以更改属性,如果元素未受影响,这也会更改属性。一旦元素离开此初始状态,更改属性就不再影响该属性。确切的行为可能因浏览器而异。
Instead of .attr('checked', 'cheked') use .prop('checked', true)
Instead of .attr('checked', 'cheked') use .prop('checked', true)