2014-09-10 136 views
-1

如果沒有選擇複選框,那麼我該如何.slideUp父母的div?如果沒有選中複選框,如何隱藏div jQuery

if ($("input:checkbox:not(:checked)")) { 
    $("#parentOfParentDiv").slideUp(); 
// this should slide up the entire parent div IF NONE is selected, doesnt work  
    } else { 
    $("input:checkbox:not(:checked)").closest('div').slideUp(); 
//this should slideUp all the divs that are NOT checked, this works 
} 
+0

你可以發佈sample'HTML'結構嗎? – 2014-09-10 13:42:18

+0

您應該檢查集合的「長度」。 '$(「input:checkbox:not(:checked)」)'返回一個對象,它是JavaScript中的一個真值。 – undefined 2014-09-10 13:42:38

回答

0

你需要這樣的:

if (!$(":checkbox").is(":checked")) { 
    $("#parentOfParentDiv").slideUp(); 
} else { $("input:checkbox:not(:checked)").closest('div').slideUp(); } 
+0

忘了添加'!',現在添加 – 2014-09-10 13:47:37

+0

謝謝,我用這個,現在它的工作原理:)真棒快速回復 – SNT 2014-09-10 13:55:06

+0

@ user3712713不要忘記標記爲答案,如果它幫助 – 2014-09-10 13:56:25

0

檢查選中的複選框

if ($("input:checkbox:checked").length >0) { 
    $("input:checkbox:not(:checked)").closest('div').slideUp(); 
} else { 
    $("#parentOfParentDiv").slideUp(); 
} 
+0

謝謝你,快速回復,幫助我。我沒有意識到,長度適用於非文本字段oto – SNT 2014-09-10 14:00:21

0

的長度,如果我理解正確的話,你應該檢查選中的複選框的長度:

if (!$("input:checkbox:checked").length) { // nothing is checked 
    $("#parentOfParentDiv").slideUp(); 
} else { 
    $("input:checkbox:not(:checked)").closest('div').slideUp(); 
}