jquery attr('checked','checked')只工作一次 [英] jquery attr('checked','checked') works only once

查看:299
本文介绍了jquery attr('checked','checked')只工作一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找以下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的所有复选框添加属性check
- 如果没有:从所有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.obj(基本上是一个ceratain div的ID)

Word of explenation: grid - is the object that I pass to get grid.obj ( basically ID of a ceratain div )

请给我你的意见。

Please give me your opinion.

推荐答案

使用 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')只工作一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆