每次運行這段代碼時,
$('#type-controls .control-case.unselected').click(function() {
新的點擊處理程序實際上是添加到您的代碼。因此,導致重複發射。我用另一種方法重寫了你的代碼。看看它是否滿足您的要求。
$(document).ready(function() {
$('input:checkbox#foam-control, input:checkbox#reversecurve-control, input:checkbox#ultra-control, input:checkbox#pro-control, input:checkbox#icebreaker-control').attr('checked', true);
$("#type-controls input[type=checkbox]").click(function() {
var countchecked = $("#type-controls input[type=checkbox]:checked").length;
if (countchecked >= 5) {
$('#type-controls input[type=checkbox]').not(':checked').attr("disabled", true);
}
else {
$('#type-controls input[type=checkbox]').not(':checked').attr("disabled", false);
}
});
$("#type-controls label").click(function() {
var countchecked = $("#type-controls input[type=checkbox]:checked").length;
if (countchecked >= 5 && $(this).parent().hasClass('unselected')) {
alert("Do notice");
$('.max-notice').animate({
'backgroundColor': '#c30c08',
'color': '#fff'
}, 400).delay(3000).animate({
'backgroundColor': '#fff',
'color': '#777'
}, 300);
}
});
});
您可以在http://jsfiddle.net/w7djF/1/
我可以向您推薦http://jsfiddle.net/您所有的網頁代碼粘貼需要觀看演示,它的方式隔開JS/CSS /標記,使結果的即時預覽。 – 2012-08-02 18:32:35
您可以使用名爲'hasRun'的布爾變量並將其初始設置爲'false',並在動畫運行一次後將其值更改爲'true' – laker 2012-08-02 18:38:56