2013-10-25 196 views
0

我一直在搞這個,弄不明白。jquery:檢查是否至少有一個.element包含至少一個.element2

不久我需要檢查某些頁面/段中的內容是否正確/它有適當的格式。

條件

  1. 必須有至少一種元素.box的
  2. 每個元素.box的必須包含類的一個或多個div元素。提問
  3. 每個。提問必須包含兩個或更多個:單選按鈕

這將是確定的(不要與元素的其餘部分相混淆,它只是真實的例子):

<div class="box" type="1"> 
     <div class="question"> 
      <div class="answers"> 
       <table> 
        <tr> 
         <td><input type="radio" name="someName" value="1" /><label>Some Label Here..</label></td> 
         <feedback>Question response 1.<strong>some quoted content in bold</strong></feedback> 
        </tr> 
        <tr> 
         <td><input type="radio" name="someName" value="1" /><label>Some Label Here..</label></td> 
         <feedback>Question response 1.<strong>some quoted content in bold</strong></feedback> 
        </tr> 
       </table> 
      </div> 
     </div> 
    </div> 
    <div class="box" type="1"> 
     <div class="question"> 
      <div class="answers"> 
       <table> 
        <tr> 
         <td><input type="radio" name="someName" value="1" /><label>Some Label Here..</label></td> 
         <feedback>Question response 1.<strong>some quoted content in bold</strong></feedback> 
        </tr> 
        <tr> 
         <td><input type="radio" name="someName" value="1" /><label>Some Label Here..</label></td> 
         <feedback>Question response 1.<strong>some quoted content in bold</strong></feedback> 
        </tr> 
       </table> 
      </div> 
     </div> 
    </div> 

但是這一次將失敗,因爲在一個DIV。提問(最後一個)它只有一個:單選按鈕,所以它不是有效的:

<div class="box" type="1"> 
     <div class="question"> 
      <div class="answers"> 
       <table> 
        <tr> 
         <td><input type="radio" name="someName" value="1" /><label>Some Label Here..</label></td> 
         <feedback>Question response 1.<strong>some quoted content in bold</strong></feedback> 
        </tr> 
        <tr> 
         <td><input type="radio" name="someName" value="1" /><label>Some Label Here..</label></td> 
         <feedback>Question response 1.<strong>some quoted content in bold</strong></feedback> 
        </tr> 
       </table> 
      </div> 
     </div> 
    </div> 
    <div class="box" type="1"> 
     <div class="question"> 
      <div class="answers"> 
       <table> 
        <tr> 
         <td><input type="radio" name="someName" value="1" /><label>Some Label Here..</label></td> 
         <feedback>Question response 1.<strong>some quoted content in bold</strong></feedback> 
        </tr> 
        <tr> 
         <td><label>Some Label Here..</label></td> 
         <feedback>Question response 1.<strong>some quoted content in bold</strong></feedback> 
        </tr> 
       </table> 
      </div> 
     </div> 
    </div> 

我想是這樣的,但它不工作我...:

  1. 如果($( '盒子')濾波器(函數(){VAR 自我= $(本); return self.find('。question')。length == 1 & & self.find('。question:radio')。length> 1; ('。') })。length> 0) { alert('NO') } else {('。box:first')。fadeIn(1000); }

  2. 和這樣的:。

    如果($( '框 ')長度){ $('。框')每個(函數(){ 如果($(」問題「),this).length){(」。question「)。each(function(){if($(':radio',this).length> 1) alert('ok') }) ; }

    }); } else { alert('!ok'); };

回答

1

.filter應該工作:

$(".box .question").filter(function() { 
    return $(this).find(":radio").length >= 2; 
}); 
+0

遺憾的是它沒有通過的情況下... :( – mariotanenbaum

+0

怎麼會這樣是什麼?發生? – tymeJV

+0

如果在.question divs之一,我只放一個:單選按鈕它仍然通過好..我試試這樣:if($(「。box .question」)。filter(function(){return $(this).find(「:radio」)。長度> = 2; })) \t \t alert('yes'); \t \t else \t \t alert('no'); – mariotanenbaum

1

嘗試

var $boxes = $('.box'), 
    valid = $boxes.length > 0; 
if (valid) { 
    $boxes.each(function (idx, box) { 
     var $box = $(this), 
      $qtns = $box.find('.question'); 
     if ($qtns.length == 0) { 
      valid = false; 
      return false; 
     } 
     valid = $qtns.filter(function() { 
      return $(this).find('input[type="radio"]').length < 2; 
     }).length == 0; 
     if (!valid) { 
      return; 
     } 
    }) 
} 
alert(valid) 

演示:Fiddle

+0

UH:D讓我測試它:D我認爲它可以是一個智能jquery鏈接線:D – mariotanenbaum

+0

@mariotanenbaum可能是....我沒有把很多想到它...... –

+0

啊,它失敗...... :(http://jsbin.com/aRuvOlA/3/edit?html,js,output – mariotanenbaum

相關問題