开发者

Jquery checkbox check / uncheck all works fine - check / uncheck one doesn't

having an issue here, I need to manually check / uncheck one checkbox. using this code it doesn't work

$('input:checkbox').live('click', function(event) { 
  event.preventDefault(); 
  if($(this).attr('checked') == false) { 
    $(this).attr('checked', true); 
  } else { 
    $(this).attr('checked', false); 
  } 
  return false; 
});

but using almost the same to check / uncheck all checkboxes work fine...

$(".todos").live('click', function(开发者_如何转开发event) {
event.preventDefault();
$.each($('input[type="checkbox"]'), function(i) {
    if($(this).hasClass('in_'+que_caixa)) {
        if($(this).attr('checked') == false) {
            $(this).attr('checked', true);
        } else {
            $(this).attr('checked', false);
        }               }
});

});

What can I do?

Thanks


You need to return false because after you do your check, it still continues through the event

$('input[type="checkbox"]').live('click', function(event) {
  if($(this).attr('checked') == false) {
    $(this).attr('checked',"checked");
  } else {
    $(this).attr('checked', false);
  }   
  return false;
});

and like the comment above the browser already does this so why do you have to bind it?

and on another note, you can actually just do this as a selector:

$("input:checkbox")


The browser already toggles the checkbox for you.
Your code toggles it a second time, so nothing happens.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜