2015-02-09 107 views
0

由于该函数执行两次,因此ToggleClass未给出预期结果。toggleClass未按预期工作:函数正在调用两次

HTML:

<form method="get" class="custom-controls"> 
    <label><input name="Gorilla" type="checkbox"/>Gorilla</label><br/> 
    <label><input name="Lamb" type="checkbox"/>Lamb</label><br/> 
    <label><input name="Tiger" type="checkbox"/>Tiger</label><br/> 
    <input type="submit"> 
</form>  

的jQuery:

$(document).ready(function(){ 
    $('.custom-controls').find('input').each(function(){ 
     ($(this).is(':radio')?$input_class_name="custom-radio-wrap":$input_class_name="custom-check-wrap");  
     $(this).parents('label').wrapInner(('<span class="' + $input_class_name + '"></span>'));      
    }); 
    $('.custom-controls').delegate("span", "click", function() { 
     $(this).toggleClass('checked');  
    }); 
}); 

FIDDLE REFERENCE

+0

检查我的回答 – 2015-02-10 08:43:12

回答

2

这是由事件冒泡引起的,只加它后面是。

$('.custom-controls').delegate("span", "click", function() { 
    $(this).toggleClass('checked'); 
    return false; 
}); 

OR 

$('.custom-controls').delegate("span", "click", function (e) { 
    $(this).toggleClass('checked'); 
    e.preventDefault();              
}); 
1

尝试使用

$(document).on('click', 'span', function(e){ 
1

终于代码将以下

$(document).ready(function() { 
 
    $('.custom-controls').find('input').each(function() { 
 
    ($(this).is(':radio') ? $input_class_name = "custom-radio-wrap" : $input_class_name = "custom-check-wrap"); 
 
    $(this).parents('label').wrapInner(('<span class="' + $input_class_name + '"></span>')); 
 
    }); 
 
    $('.custom-controls').delegate("span", "click", function(e) { 
 
    $(this).toggleClass('checked'); 
 
    var checked_status = $(this).find("input").prop("checked") ? 0 : 1; //for checked input checkbox.. 
 
    $(this).find("input").prop("checked", checked_status); 
 
    e.preventDefault(); 
 
    }); 
 
});
.custom-label { 
 
    display: inline-block; 
 
    margin-bottom: .8em; 
 
    cursor: pointer; 
 
} 
 
.custom-radio, 
 
.custom-check { 
 
    vertical-align: middle; 
 
    display: inline-block; 
 
    position: relative; 
 
    top: -.15em; 
 
    margin: 0 .4em; 
 
    width: 22px; 
 
    height: 22px; 
 
} 
 
label { 
 
    display: block; 
 
} 
 
.custom-check-wrap input { 
 
    /* left:-9999px;*/ 
 
    position: absolute; 
 
} 
 
.custom-check-wrap { 
 
    background: #CCC; 
 
    padding-left: 30px; 
 
    min-height: 22px; 
 
    display: block; 
 
} 
 
.custom-check-wrap.checked { 
 
    background: green; 
 
} 
 
.custom-check { 
 
    background: url(images/chk-uncheck.png) no-repeat; 
 
} 
 
.custom-check.checked { 
 
    background: url(images/chk-checked.png) no-repeat; 
 
} 
 
.custom-radio { 
 
    background: url(images/radio-uncheck.png) no-repeat; 
 
} 
 
.custom-radio.checked { 
 
    background: url(images/radio-checked.png) no-repeat; 
 
} 
 
.custom-check.focus { 
 
    background: url(images/chk-uncheck.png) no-repeat; 
 
} 
 
.custom-check.checked.focus { 
 
    background: url(images/chk-checked.png) no-repeat; 
 
} 
 
.custom-radio.focus { 
 
    background: url(images/radio-uncheck.png) no-repeat; 
 
} 
 
.custom-radio.checked.focus { 
 
    background: url(images/radio-checked.png) no-repeat; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="custom-controls"> 
 
    <form method="get"> 
 
    <label> 
 
     <input name="Gorilla" type="checkbox" />Gorilla</label> 
 
    <br/> 
 
    <label> 
 
     <input name="Lamb" type="checkbox" />Lamb</label> 
 
    <br/> 
 
    <label> 
 
     <input name="Tiger" type="checkbox" />Tiger</label> 
 
    <br/> 
 
    <input type="submit" /> 
 
    </form> 
 
</div>

+0

是的,我upvoted它已经:)感谢的人 – Husen 2015-02-10 12:50:38

+0

OK也谢谢你... – 2015-02-10 12:52:10