我試圖讓這個事件停止傳播到更遠的孩子,但沒有任何成功。
除非單擊文本,否則無法選中復選框 3,4 和 5:
$(document).on("touchend click", ".filter-custom", function() {
if($(this).children("input").prop("checked") == true){
$(this).children("input").prop( "checked", false );
event.stopPropagation();
event.preventDefault ();
}else{
$(this).children("input").prop( "checked", true );
event.stopPropagation();
event.preventDefault ();
}
});
這是關于 JSFiddle 的完整示例https://jsfiddle.net/ot9y80jr/1/
uj5u.com熱心網友回復:
好的,這是一種解決方法,當單擊它自身的復選框時,您的函式會重疊,因為它集中在 li 元素上,因此每次單擊該元素都會導致取消選中/選中復選框。
此解決方案通過查看事件目標文本來作業,如果有文本他繼續檢查/取消選中復選框,否則什么都不做而不重疊
$(document).on("touchend click", ".filter-custom", function(event) {
if($(event.target).text()){
if($(this).children("input").prop("checked") == true){
$(this).children("input").prop( "checked", false );
event.stopPropagation();
event.preventDefault ();
}else{
$(this).children("input").prop( "checked", true );
event.stopPropagation();
event.preventDefault ();
}
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/485386.html
上一篇:如何在jQuery中添加媒體查詢
