2015-02-09 49 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