jQuery checkbox not responding when I check it
I have that function:
$("#tabela tr").toggle(function(){
$(this).addClass("zaznaczone");
$(this).find(" :checkbox").attr("checked", "1");
},function(){
$(this).removeClass("zaznaczone");
$(this).find(" :checkbox").attr("checked", "");
});
and it's fine when i click on "tr" but when i click on checkbox this line response but checkbox not. It's mean checkobx after this operation is not checked.
i write bottom this code:
$("#tabela tr :checkbox").click(function(){
if($(this).attr('checked')==''){
alert("000000开发者_运维问答");
}
else{
alert("111111111");
}
});
and what happen. When i click on empty checkbox i see alert 1111111 and before press ok on alert popup checkbox is checked. After press ok checkbox is not checked and tr get class from code up. When i try uncheck checkbox i see this same alert 00000 and line "tr" is unset with code up
Your code work perfect for line and checkbox but when I try add this lines:
$(this).addClass("zaznaczone");
$(this).removeClass("zaznaczone");
then work that: When i click on line work that what it should, but when i click on checkbox then check it addClass dont work All code:
$('#tabela tr').bind('click',function(e){
var cb = $(this).find(':checkbox');
if(e.target !== cb[0]){
if(cb.is(':checked')){
cb.removeAttr('checked');
$(this).removeClass("zaznaczone");}
else{
cb.attr('checked', 'checked');
$(this).addClass("zaznaczone");}
}
});
I wasn't able to make this work with .toggle()
, it will call .preventDefault()
on it's own which just makes this task awesome for some reason. Maybe I'm just missing something, but the code below is just fine and works.
$(document).ready(function(){
$('tr').bind('click',function(e){
var cb = $(this).find(':checkbox');
if(e.target !== cb[0]){
if(cb.is(':checked'))
cb.removeAttr('checked');
else
cb.attr('checked', 'checked');
}
});
});
精彩评论