我已经搞砸了一段时间,无法弄清楚。
不久,我需要检查某些页面/段中的内容是否正确/是否具有适当的格式。
条件
- 必须至少有一个元素 .box
- 每个元素 .box 必须包含一个或多个 .question 类的 div 元素
- 每个 .question 必须包含两个或更多:单选按钮
这没关系(不要与其他元素混淆,这只是真实的例子):
<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>
但是这个会失败,因为在一个 .question div(最后一个)中它只有一个 :radio 按钮,所以它无效:
<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>
我尝试过这样的事情,但它对我不起作用......:
if($('.box').filter(function() { var self = $(this); return self.find('.question').length == 1 && self.find('.question :radio' ).length > 1; }).length > 0) { alert('NO') } else { $('.box:first').fadeIn(1000); }
和这个:
if ($('.box').length) { $('.box').each(function(){ if ($(".question", this).length) { $(".question"). each(function(){ if($(':radio', this).length > 1) alert('ok') }); }
}); } 其他 { 警报('!ok');};