我在运行时生成几个框,我想确认所有框是否包含文本“Empty”,然后用户不能继续.但即使一个Box包含正确的Box值(而不是Empty),用户也应该可以继续.
请在下面找到我的代码:
<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 & 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/
如果您需要任何其他信息,请告诉我.
请指教!
解决方法
如果你想要做的事情,如果至少一个盒子包含“空”以外的东西,你不需要每个功能.这样做:
if ($('.bin-area').length === $('.bin-area:contains("Empty")').length) { alert("Do nothing"); } else { alert("Do something"); }