2016-04-13 19 views
2

İlk kapalı, belirsiz soru için özür dilerim, bunları kelimelere nasıl yazacağımı hiç bilmiyordum. Benim sorunum en iyi örnekle gösterilmektedir.Özel CSS onay kutusu jQuery tarafından tetiklenen "işaretli" özellik, görsel denetim kutusunu "onaylama" onay kutusunu işaretlemiyor

İçinde birkaç onay kutusu bulunan bir bootstrap açılır menüsü kullanıyorum. Bazı güzel FontAwesome simgeleri kullanarak, görünümlerini değiştirmek için bir css hile kullandım. Ancak jQuery kullanarak bu onay kutularının devlet ile bir şeyler yapmaya çalışıyorum, ilk çek/işaretini kaldırın iş gibi görünüyor, ancak herhangi bir sonraki geçiş (kaldırın/kontrol edin) ... çalışmıyor

$('.js-inputhit').click(function() { 
 
    if (this === event.target) { 
 
    $(this).find(':input').click(); 
 
    } 
 
}); 
 
$('.js-inputhit :input').click(function(e) { 
 
    e.stopPropagation(); 
 
}); 
 

 
$('.selectchat_all').click(function() { 
 
    $('.selectchat_active').attr('checked', false); 
 
    $('#chat1, #chat2, #chat3, #chat4').attr('checked', true); 
 
}); 
 
$('.selectchat_active').click(function() { 
 
    $('.selectchat_all').attr('checked', false); 
 
    $('#chat1, #chat2, #chat3, #chat4').attr('checked', false); 
 
});
/*Adding custom checkbox icons*/ 
 

 
label { 
 
    position: relative; 
 
    padding-left: 20px; 
 
    cursor: pointer; 
 
} 
 
label:before, 
 
label:after { 
 
    font-family: FontAwesome; 
 
    font-size: 14px; 
 
    /*absolutely positioned*/ 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
} 
 
label:before { 
 
    content: '\f096'; 
 
    /*unchecked*/ 
 
} 
 
label:after { 
 
    content: '\f046'; 
 
    /*checked*/ 
 
    /*checked icon will be hidden by default by using 0 max-width and overflow hidden*/ 
 
    max-width: 0; 
 
    overflow: hidden; 
 
    opacity: 0.5; 
 
    /*CSS3 transitions for animated effect*/ 
 
    transition: all 0.35s; 
 
} 
 
/*hiding the original checkboxes*/ 
 

 
input[type="checkbox"] { 
 
    display: none; 
 
} 
 
/*when the user checks the checkbox the checked icon will animate in*/ 
 

 
input[type="checkbox"]:checked + label:after { 
 
    max-width: 25px; 
 
    /*an arbitratry number more than the icon's width*/ 
 
    opacity: 1; 
 
    /*for fade in effect*/ 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" /> 
 
<input type="checkbox" name="chat1" id="chat1" checked/> 
 
<label for="chat1" style="font-weight: normal; margin-bottom: 0px;">DreamhackCS</label> 
 
<br> 
 

 
<input type="checkbox" name="chat2" id="chat2" /> 
 
<label for="chat2" style="font-weight: normal; margin-bottom: 0px;">IzakOOO</label> 
 
<br> 
 

 
<input type="checkbox" name="chat3" id="chat3" /> 
 
<label for="chat3" style="font-weight: normal; margin-bottom: 0px;">Nightblue3</label> 
 
<br> 
 

 
<input type="checkbox" name="chat4" id="chat4" /> 
 
<label for="chat4" style="font-weight: normal; margin-bottom: 0px;">imaqtpie</label> 
 
<br><br> 
 

 
<input type="checkbox" name="active" class="selectchat_active" id="active" /> 
 
<label for="active" style="font-weight: normal; margin-bottom: 0px;">Check none</label> 
 
<br> 
 

 
<input type="checkbox" name="all" class="selectchat_all" id="all"/> 
 
<label for="all" style="font-weight: normal; margin-bottom: 0px;">Check all</label>
Bu örnekte

sorun: tıklamak tüm 4 kanal denetler "tüm denetle". "Hiçbirini kontrol et" i tıklamak, tümünü kontrol ediyor. (çok uzak çok iyi). Ancak bundan sonra "Tümünü Kontrol Et" artık çalışmıyor. Artık 4 kanalı kontrol etmiyor. "Hiçbiri kontrol et" hala hepsini kontrol etmiyor (eğer manuel olarak kontrol ediyorsanız), ama "Tümünü Kontrol Et" artık çalışmıyor ...

Sorunumu bulabilen biri var mı :)? Yardımınız için çok teşekkür ederim.

cevap

7

Kullanım .prop() yerine .attr() ait

attr() yalnızca bkz fazla bilgi için fiili DOM ağacını

günceller http://api.jquery.com/prop/ veya this answer

$('.js-inputhit').click(function() { 
 
    if (this === event.target) { 
 
    $(this).find(':input').click(); 
 
    } 
 
}); 
 
$('.js-inputhit :input').click(function(e) { 
 
    e.stopPropagation(); 
 
}); 
 

 
$('.selectchat_all').click(function() { 
 
    $('.selectchat_active').prop('checked', false); 
 
    $('#chat1, #chat2, #chat3, #chat4').prop('checked', true); 
 
}); 
 
$('.selectchat_active').click(function() { 
 
    $('.selectchat_all').prop('checked', false); 
 
    $('#chat1, #chat2, #chat3, #chat4').prop('checked', false); 
 
});
/*Adding custom checkbox icons*/ 
 

 
label { 
 
    position: relative; 
 
    padding-left: 20px; 
 
    cursor: pointer; 
 
} 
 
label:before, 
 
label:after { 
 
    font-family: FontAwesome; 
 
    font-size: 14px; 
 
    /*absolutely positioned*/ 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
} 
 
label:before { 
 
    content: '\f096'; 
 
    /*unchecked*/ 
 
} 
 
label:after { 
 
    content: '\f046'; 
 
    /*checked*/ 
 
    /*checked icon will be hidden by default by using 0 max-width and overflow hidden*/ 
 
    max-width: 0; 
 
    overflow: hidden; 
 
    opacity: 0.5; 
 
    /*CSS3 transitions for animated effect*/ 
 
    transition: all 0.35s; 
 
} 
 
/*hiding the original checkboxes*/ 
 

 
input[type="checkbox"] { 
 
    display: none; 
 
} 
 
/*when the user checks the checkbox the checked icon will animate in*/ 
 

 
input[type="checkbox"]:checked + label:after { 
 
    max-width: 25px; 
 
    /*an arbitratry number more than the icon's width*/ 
 
    opacity: 1; 
 
    /*for fade in effect*/ 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" /> 
 
<input type="checkbox" name="chat1" id="chat1" checked/> 
 
<label for="chat1" style="font-weight: normal; margin-bottom: 0px;">DreamhackCS</label> 
 
<br> 
 

 
<input type="checkbox" name="chat2" id="chat2" /> 
 
<label for="chat2" style="font-weight: normal; margin-bottom: 0px;">IzakOOO</label> 
 
<br> 
 

 
<input type="checkbox" name="chat3" id="chat3" /> 
 
<label for="chat3" style="font-weight: normal; margin-bottom: 0px;">Nightblue3</label> 
 
<br> 
 

 
<input type="checkbox" name="chat4" id="chat4" /> 
 
<label for="chat4" style="font-weight: normal; margin-bottom: 0px;">imaqtpie</label> 
 
<br><br> 
 

 
<input type="checkbox" name="active" class="selectchat_active" id="active" /> 
 
<label for="active" style="font-weight: normal; margin-bottom: 0px;">Check none</label> 
 
<br> 
 

 
<input type="checkbox" name="all" class="selectchat_all" id="all"/> 
 
<label for="all" style="font-weight: normal; margin-bottom: 0px;">Check all</label>

+0

Teşekkürler! Hiç bir fikrim yoktu :) Şimdi çalışıyor. Yapabileceğim zaman cevabınızı doğru seçecek (7 dakika sonra). –

+1

Sorunu çözdüğüne sevindim. Bu stackoverflow için ne olduğunu :) –

2

.attr yerine .prop kullanmayı deneyin:

$('#chat1, #chat2, #chat3, #chat4').prop('checked', true); 

ek bilgi için this answer bakın.

+0

Harika, bu işe yarıyor. Farkın açıklığa kavuşturulması için teşekkür ederiz. –

İlgili konular