0

私はこれをしばらくいじっていましたが、理解できません。

すぐに、特定のページ/セグメントのコンテンツが適切かどうか、または適切な形式であるかどうかを確認する必要があります。

条件

  1. 少なくとも 1 つの要素 .box が必要です
  2. 各要素 .box には、クラス .question の 1 つ以上の div 要素が含まれている必要があります
  3. 各 .questions には 2 つ以上の :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><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>

ただし、1 つの .question div (最後の 1 つ) には :radio ボタンが 1 つしかないため、有効ではないため、これは失敗します。

<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') }); }

    }); } else { アラート('!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 に答える