3

実行時にいくつかのボックスを生成します。すべてのボックスに「空」というテキストが含まれているかどうかを確認したい場合、ユーザーは続行できません。ただし、1 つのボックスでも (Empty ではなく) box の正しい値が含まれている場合、ユーザーは続行できるはずです。

以下のコードを見つけてください:

HTML

<div>
    <div class="bin-area empty floatLeft" style="width:242px; height:130px; ">
        <label for="9">
            <div class="bin" data-binid="0" data-cols="0" data-rows="0">
                <div class="number">S9</div>
                <input class="floatLeft styled" id="9" name="checkbox9" type="checkbox" />
                <label for="9"><span class="floatLeft marginLeft40">Empty</span>

                </label>
                <div class="type"></div>
                <div class="description">Storage</div>
            </div>
        </label>
    </div>
    <div class="bin-area empty floatLeft" style="width:242px; height:130px; ">
        <label for="9">
            <div class="bin" data-binid="0" data-cols="0" data-rows="0">
                <div class="number">S9</div>
                <input class="floatLeft styled" id="9" name="checkbox9" type="checkbox" />
                <label for="9"><span class="floatLeft marginLeft40">Empty</span>

                </label>
                <div class="type"></div>
                <div class="description">Storage</div>
            </div>
        </label>
    </div>
    <div class="bin-area" style=" width:242px; height:130px; ">
        <label for="8">
            <div class="bin" data-binid="5" data-cols="9" data-rows="5">
                <div class="number">S8</div>
                <input class="floatLeft styled" id="8" name="checkbox8" type="checkbox" />
                <div class="type">9x5 Storage Bin</div>
                <div class="description">Est. Capacity: 121 pkgs</div>
            </div>
        </label>
    </div>
</div>
<div style="clear:both"></div>
<div role="button" style="margin-top:20px">
    <input type="button" id="stepAutomap" value="Automap" class="active" />
    <input type="button" id="stepAutomapBack" value="Back" class="active marginLeft50" />
    <input type="button" id="stepAutomapConfirm" value="Confirm &amp; Proceed" class="marginLeft10" />
</div>

これは私の HTML 構造です...既存の投稿のいくつかを検索して、jQuery を使用していくつかの IF ロジックを作成してみました:

私はこのオプションを試しました:

$(document).ready(function () {
    $('.bin-area').each(function () {
        if ($(this).text() == 'Empty') {
            alert("Empty");
        }
    });
});

そして、このオプション:

$(document).ready(function () {
    if ($("span.floatLeft").contains("Empty")) {
        alert("Empty");
    }
});

しかし、何もうまくいきませんでした!

参照または試してみるフィドルも作成しました。

フィドルを参照してください: http://jsfiddle.net/aasthatuteja/xJtAV/

他に情報が必要な場合はお知らせください。

提案してください!

4

5 に答える 5

6

少なくとも 1 つのボックスに「空」以外のものが含まれている場合に何かを実行したい場合は、 each 関数は必要ありません。これを行うだけです:

if ($('.bin-area').length === $('.bin-area:contains("Empty")').length) {
    alert("Do nothing");
}
else {
    alert("Do something");
}
于 2013-10-07T20:01:11.360 に答える
4

.contains()セレクターを次のように使用できます

 $('.bin-area:contains("Empty")')

変更されたコード

$('.bin-area:contains("Empty")').each(function () {
    alert("Empty");
});

デモ

于 2013-10-07T19:51:47.213 に答える