我一直在弄乱这一点,无法弄明白。
我很快就需要检查某个页面/细分中的内容是否合适/它是否具有适当的格式。
条件
这没关系(不要与其他元素混淆,这只是一个真实的例子):
<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(最后一个)中它只有一个:单选按钮所以它无效:
<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&amp;&amp; self.find('。question:radio')。length&gt; 1; })。长度&gt; 0) { 警报(“否”) } else { $( '框:第一')淡入(1000)。 }
并且:
if($('。box')。length){ $( '盒子')。每个(函数(){ if($(“。question”,this).length){ $( “问题”)。每个(函数(){ if($(':radio',this)。length&gt; 1) 警报( 'OK') }); }
}); } else { 警报( 'OK!'); };
答案 0 :(得分: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
答案 1 :(得分:0)
.filter
应该有效:
$(".box .question").filter(function() {
return $(this).find(":radio").length >= 2;
});