0

我已经搞砸了一段时间,无法弄清楚。

不久,我需要检查某些页面/段中的内容是否正确/是否具有适当的格式。

条件

  1. 必须至少有一个元素 .box
  2. 每个元素 .box 必须包含一个或多个 .question 类的 div 元素
  3. 每个 .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>

我尝试过这样的事情,但它对我不起作用......:

  1. 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); }

  2. 和这个:

    if ($('.box').length) { $('.box').each(function(){ if ($(".question", this).length) { $(".question"). each(function(){ if($(':radio', this).length > 1) alert('ok') }); }

    }); } 其他 { 警报('!ok');};

4

2 回答 2

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)

演示:小提琴

于 2013-10-25T16:15:25.833 回答
0

.filter应该管用:

$(".box .question").filter(function() {
    return $(this).find(":radio").length >= 2;
});
于 2013-10-25T15:42:47.277 回答