2014-05-07 170 views
0

我已經打開了一個類似的線程,除非div沒有第二個元素。 所以我的最後一個問題是這樣的:檢查div是否有元素並且沒有某些元素

我有一些代碼,看起來像這樣

<p class="elementWrap"> 
    <label>Phone</label> 
    <input class="form_elem" type="text" name="form_phone"> 
    <span class="form_required">*</span> 
</p> 

,有時像這樣

<p class="elementWrap"> 
    <label>Name</label> 
    <input class="form_elem" type="text" name="form_name"> 
</p> 

rgraham張貼了這個解決方案,它工作正常

$(".elementWrap").filter(function() { 
    return $(this).children(".form_required").length; 
}).find("label").css({'width':'auto','margin':'0px'}); 

但現在我不想在標籤whi上應用此代碼ch有無線電類型的輸入。 我想過的東西就像返回兒童.form_required的長度,而不是輸入類型的收音機。但不知道如何把它寫成代碼。 =/

+0

請看我最後的更新。這是有用的和少編碼 –

回答

1
$(".elementWrap").filter(function() { 
    var has_required = $(this).children(".form_required").length > 0, 
     no_radio = $(this).find(':radio').length < 1; 

    return has_required && no_radio; 
}) 
+0

這完美的作品! :) – Thomas

0

從jQuery文檔:

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>has demo</title> 
    <style> 
     .full { 
      border: 1px solid red; 
     } 
    </style> 
    <script src="//code.jquery.com/jquery-1.10.2.js"></script> 
</head> 
<body> 
<ul> 
    <li>Does the UL contain an LI?</li> 
</ul> 
<script> 
    $("ul").append("<li>" + 
     ($("ul").has("li").length ? "Yes" : "No") + 
     "</li>"); 
    $("ul").has("li").addClass("full"); 
</script> 
</body> 
</html> 

http://api.jquery.com/has/

0
$(".elementWrap:has(.form_required)").filter($(".elementWrap:not(:has(:radio))")) 

已知$one.filter($two)$one ∩ $two

DEMO