Некоторое время я занимался этим, не могу понять.
Вскоре мне нужно проверить, соответствует ли контент на определенной странице/сегменте/имеет соответствующий формат.
условия
Это было бы нормально (не путайте с остальными элементами, это просто настоящий пример):
<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 && 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"). каждый (функция() {if ($ (': radio', this).length> 1) alert ('ok')});}
}); } else {alert ('! ok'); }; };
Пытаться
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)
Демо: скрипка
.filter
должен работать:
$(".box .question").filter(function() {
return $(this).find(":radio").length >= 2;
});