2015-10-14 170 views
0

我的问题与Targeting $(this) within nested for each loops in jQuery完全一样,但是由于某种原因,我无法使其工作。这里是我的代码:针对jQuery中每个循环的嵌套子元素

$(document).ready(function() { 
 
    var rows=$('.row'); 
 
    var count = 0; 
 
    $.each(rows, function() { 
 
     console.log(".row "+count++); 
 
     var row = $(this); 
 
     var cells = row.find('span.RIF_Field'); 
 
     if (cells.length > 0) { 
 
      console.log("cells "+cells.length); 
 
      var hidden = true; 
 
      $.each(cells, function() { 
 
       if(!$(this).is(':hidden')) { 
 
        console.log($(this).id + " is visible"); 
 
        hidden = false; 
 
       } 
 
      }); 
 
      if (hidden) { 
 
       console.log("nothing is visible"); 
 
       row.hide(); 
 
      } 
 
     } 
 
    }); 
 
});
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="container"> 
 
    
 
<div class="row"><div class="col-sm-6"><br style="clear:both;"> 
 
<div class="RIF_Field RIF_Street" id="yourStreet" style="float:left;width:200px;"><label class="fldLbl" id="yourStreetLabel">Address Line 1:</label><br> 
 
<span style="white-space: nowrap;"><input id="yourStreetValue" maxlength="35" name="yourStreet" size="32" style="width:200px;" type="text"><img alt="" height="9" id="yourStreetReqdImg" src="/img/1.gif" width="11"></span></div> 
 
</div> 
 
<div class="col-sm-6"><br style="clear:both;"> 
 
<div class="RIF_Field RIF_Addr2" id="yourAddr2" style="float:left;width:200px;"><label class="fldLbl" id="yourAddr2Label">Address Line 2:</label><br> 
 
<span style="white-space: nowrap;"><input id="yourAddr2Value" maxlength="35" name="yourAddr2" size="32" style="width:200px;" type="text"><img alt="" height="9" id="yourAddr2ReqdImg" src="/img/1.gif" width="11"></span><br> 
 
<span class="RIF_description">Test</span></div> 
 
</div> 
 
</div> 
 
    
 
    
 
<div class="row"><div class="col-sm-6"><br style="clear:both;"> 
 
<div class="RIF_Csp3" id="yourCsp3" style="float:left;"><span class="RIF_Field RIF_State" id="yourState" style="display: none;"><label class="fldLbl" id="yourStateLabel">Other Address Information:</label><br> 
 
<span style="white-space: nowrap;"><input type="text" size="15" maxlength="15" id="yourStateValue" name="yourState" onchange="null" onfocus="null" onblur="null" tabindex="0" class="ui-autocomplete-input" autocomplete="off"><img alt="" height="9" id="yourStateReqdImg" src="/img/1.gif" width="11"></span></span></div> 
 
</div> 
 
<div class="col-sm-6"><div></div> 
 
</div> 
 
</div> 
 
    
 
<div class="row"><div class="col-sm-6"><br style="clear:both;"> 
 
<div class="RIF_Field RIF_Phone" id="yourPhone" style="float:left;width:300px;"><label class="fldLbl" id="yourPhoneLabel">Telephone:</label><br> 
 
<span style="white-space: nowrap;"><input id="yourPhoneValue" maxlength="15" name="yourPhone" size="15" style="width:300px;" type="text"><img alt="" height="9" id="yourPhoneReqdImg" src="/img/1.gif" width="11"></span></div> 
 
<div class="RIF_Field RIF_Extension" id="yourExtension" style="float:left;width:100px;"><label class="fldLbl" id="yourExtensionLabel">Ext.:</label><br> 
 
<span style="white-space: nowrap;"><input id="yourExtensionValue" maxlength="5" name="yourExtension" size="5" style="width:100px;" type="text"><img alt="" height="9" id="yourExtensionReqdImg" src="/img/1.gif" width="11"></span></div> 
 
</div> 
 
<div class="col-sm-6"><br style="clear:both;"> 
 
<div class="RIF_Field RIF_Email" id="yourEmail" style="float:left;width:400px;"><label class="fldLbl" id="yourEmailLabel">E-mail:</label><br> 
 
<span style="white-space: nowrap;"><input id="yourEmailValue" maxlength="50" name="yourEmail" size="32" style="width:400px;" type="text"><img alt="" height="9" id="yourEmailReqdImg" src="/img/1.gif" width="11"></span></div> 
 
</div> 
 
</div> 
 
    
 
    
 
</div>

我试图隐藏具有隐藏所有细胞.row元素。我究竟做错了什么?

+0

在情况下,我并不清楚,我的问题是,包含行“其它地址信息:”应该被隐藏,以避免在视图的差距。此外,控制台输出没有任何意义。 –

回答

0

该代码cells.size()无效,您需要cells.length。它也永远不会为空。

$(document).ready(function() { 
 
    var rows=$('.row'); 
 
    var count = 0; 
 
    $.each(rows, function() { 
 
    console.log(".row "+count++); 
 
    var row = $(this); 
 
    var cells = row.find('span.RIF_Field'); 
 
    if (cells.length > 0) { 
 
     console.log("cells "+cells.length); 
 
     var hidden = true; 
 
     $.each(cells, function() { 
 
     if(!$(this).is(':hidden')) 
 
      console.log($(this).id + " is visible"); 
 
     hidden = false; 
 
     }); 
 
     if (hidden) { 
 
     console.log("nothing is visible"); 
 
     row.hide(); 
 
     } 
 
    } 
 
    }); 
 
});
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="container"> 
 

 
    <div class="row"><div class="col-sm-6"><br style="clear:both;"> 
 
    <div class="RIF_Field RIF_Street" id="yourStreet" style="float:left;width:200px;"><label class="fldLbl" id="yourStreetLabel">Address Line 1:</label><br> 
 
     <span style="white-space: nowrap;"><input id="yourStreetValue" maxlength="35" name="yourStreet" size="32" style="width:200px;" type="text"><img alt="" height="9" id="yourStreetReqdImg" src="/img/1.gif" width="11"></span></div> 
 
    </div> 
 
    <div class="col-sm-6"><br style="clear:both;"> 
 
     <div class="RIF_Field RIF_Addr2" id="yourAddr2" style="float:left;width:200px;"><label class="fldLbl" id="yourAddr2Label">Address Line 2:</label><br> 
 
     <span style="white-space: nowrap;"><input id="yourAddr2Value" maxlength="35" name="yourAddr2" size="32" style="width:200px;" type="text"><img alt="" height="9" id="yourAddr2ReqdImg" src="/img/1.gif" width="11"></span><br> 
 
     <span class="RIF_description">Test</span></div> 
 
    </div> 
 
    </div> 
 

 

 
    <div class="row"><div class="col-sm-6"><br style="clear:both;"> 
 
    <div class="RIF_Csp3" id="yourCsp3" style="float:left;"><span class="RIF_Field RIF_State" id="yourState" style="display: none;"><label class="fldLbl" id="yourStateLabel">Other Address Information:</label><br> 
 
     <span style="white-space: nowrap;"><input type="text" size="15" maxlength="15" id="yourStateValue" name="yourState" onchange="null" onfocus="null" onblur="null" tabindex="0" class="ui-autocomplete-input" autocomplete="off"><img alt="" height="9" id="yourStateReqdImg" src="/img/1.gif" width="11"></span></span></div> 
 
    </div> 
 
    <div class="col-sm-6"><div></div> 
 
    </div> 
 
    </div> 
 

 
    <div class="row"><div class="col-sm-6"><br style="clear:both;"> 
 
    <div class="RIF_Field RIF_Phone" id="yourPhone" style="float:left;width:300px;"><label class="fldLbl" id="yourPhoneLabel">Telephone:</label><br> 
 
     <span style="white-space: nowrap;"><input id="yourPhoneValue" maxlength="15" name="yourPhone" size="15" style="width:300px;" type="text"><img alt="" height="9" id="yourPhoneReqdImg" src="/img/1.gif" width="11"></span></div> 
 
    <div class="RIF_Field RIF_Extension" id="yourExtension" style="float:left;width:100px;"><label class="fldLbl" id="yourExtensionLabel">Ext.:</label><br> 
 
     <span style="white-space: nowrap;"><input id="yourExtensionValue" maxlength="5" name="yourExtension" size="5" style="width:100px;" type="text"><img alt="" height="9" id="yourExtensionReqdImg" src="/img/1.gif" width="11"></span></div> 
 
    </div> 
 
    <div class="col-sm-6"><br style="clear:both;"> 
 
     <div class="RIF_Field RIF_Email" id="yourEmail" style="float:left;width:400px;"><label class="fldLbl" id="yourEmailLabel">E-mail:</label><br> 
 
     <span style="white-space: nowrap;"><input id="yourEmailValue" maxlength="50" name="yourEmail" size="32" style="width:400px;" type="text"><img alt="" height="9" id="yourEmailReqdImg" src="/img/1.gif" width="11"></span></div> 
 
    </div> 
 
    </div> 
 
</div>

+0

谢谢你的建议,但它不工作。 –

+0

我期望包含“其他地址信息”的.row作为'row.hide()'的结果被隐藏,以及_nothing在控制台中是可见的 –

+0

你永远不会调用'row.hide()',因为'隐藏“在该条件之上的循环中设置为false。只要'单元格'中有一个或多个项目,'hidden'始终设置为false。也许你希望'hidden = false'被设置为直接在它上面的if语句的一部分。 – dave