循环遍历元素id

时间:2013-07-16 13:44:00

标签: jquery loops remove-if

我有这个jQuery代码来检测列中的所有值是否为空,如果是,则删除列:

var valid=0;
jQuery("#column1 input[type=text]").each(function(){
    if(jQuery(this).val() != "") valid+=1;
});

if(valid ==0){
    jQuery("td#column1").hide();
}

var valid1=0;
jQuery("#column2 input[type=text]").each(function(){
    if(jQuery(this).val() != "") valid1+=1;
});

if(valid1 ==0){
    jQuery("td#column2").hide();
}

是否有任何方法可以将其转换为循环,以便它遍历每个列(#column1, #column2, etc..)并在需要时删除该列,s我不必为每列复制上述代码?< / p>

提前感谢您的帮助。

修改

感谢所有建议。 一些答案的问题是循环将删除单个单元格。如果标题下的所有字段都是空的,如果它们删除包括标题的整个列,我需要循环来计算。

<table>
<tr id="prod_specs_header">
<td><span class="tooltips-link" title="">Product Code</span></td>
<td id="column1"><span class="tooltips-link isimg" title=""><img style="opacity: 1; visibility: visible;" src="templates/autcastorsandwheels/images/icon_wheel_diam.gif" data-src="templates/autcastorsandwheels/images/icon_wheel_diam.gif" border="0"></span></td>
<td id="column2"><span class="tooltips-link isimg" title=""><img style="opacity: 1; visibility: visible;" src="templates/autcastorsandwheels/images/icon_wheel_width.gif" data-src="templates/autcastorsandwheels/images/icon_wheel_width.gif" border="0"></span></td>
<td id="column3"><span class="tooltips-link isimg" title=""><img style="opacity: 1; visibility: visible;" src="templates/autcastorsandwheels/images/icon_overall_height.gif" data-src="templates/autcastorsandwheels/images/icon_overall_height.gif" border="0"></span></td>
<td id="column4"><span class="tooltips-link isimg" title=""><img style="opacity: 1; visibility: visible;" src="templates/autcastorsandwheels/images/icon_plate_dimen.gif" data-src="templates/autcastorsandwheels/images/icon_plate_dimen.gif" border="0"></span></td>
<td id="column5"><span class="tooltips-link isimg" title=""><img style="opacity: 1; visibility: visible;" src="templates/autcastorsandwheels/images/icon_hole_spacing.gif" data-src="templates/autcastorsandwheels/images/icon_hole_spacing.gif" border="0"></span></td>
<td id="column6"><span class="tooltips-link isimg" title=""><img style="opacity: 1; visibility: visible;" src="templates/autcastorsandwheels/images/icon_hole_width.gif" data-src="templates/autcastorsandwheels/images/icon_hole_width.gif" border="0"></span></td>
<td id="column7"><span class="tooltips-link isimg" title=""><img style="opacity: 1; visibility: visible;" src="templates/autcastorsandwheels/images/icon_offset.gif" data-src="templates/autcastorsandwheels/images/icon_offset.gif" border="0"></span></td>
<td id="column8"><span class="tooltips-link isimg" title=""><img style="opacity: 1; visibility: visible;" src="templates/autcastorsandwheels/images/icon_carrycap.gif" data-src="templates/autcastorsandwheels/images/icon_carrycap.gif" border="0"></span></td>
<td id="column9"><span class="tooltips-link isimg" title=""><img style="opacity: 1; visibility: visible;" src="templates/autcastorsandwheels/images/icon_unit_weight.gif" data-src="templates/autcastorsandwheels/images/icon_unit_weight.gif" border="0"></span></td>
</tr>

<tr>
<td>37TAS5700</td>
<td class="field" id="column1"><input value="50" type="text"></td>
<td class="field" id="column2"><input value="" type="text"></td>
<td class="field" id="column3"><input value="71" type="text"></td>
<td class="field" id="column4"><input value="" type="text"></td>
<td class="field" id="column5"><input value="46/38x46/38" type="text"></td>
<td class="field" id="column6"><input value="" type="text"></td>
<td class="field" id="column7"><input value="" type="text"></td>
<td class="field" id="column8"><input value="40" type="text"></td>
<td class="field" id="column9"><input value="0.14" type="text"></td>
</tr>

<tr>
<td>37TAS5701</td>
<td class="field" id="column1"><input value="75" type="text"></td>
<td class="field" id="column2"><input value="" type="text"></td>
<td class="field" id="column3"><input value="100" type="text"></td>
<td class="field" id="column4"><input value="" type="text"></td>
<td class="field" id="column5"><input value="46/38x46/38" type="text"></td>
<td class="field" id="column6"><input value="" type="text"></td>
<td class="field" id="column7"><input value="" type="text"></td>
<td class="field" id="column8"><input value="50" type="text"></td>
<td class="field" id="column9"><input value="0.34" type="text"></td>
</tr>

<tr><td>37TAS5702</td>
<td class="field" id="column1"><input value="100" type="text"></td>
<td class="field" id="column2"><input value="" type="text"></td>
<td class="field" id="column3"><input value="122" type="text"></td>
<td class="field" id="column4"><input value="" type="text"></td>
<td class="field" id="column5"><input value="46/38x46/38" type="text"></td>
<td class="field" id="column6"><input value="" type="text"></td>
<td class="field" id="column7"><input value="" type="text"></td>
<td class="field" id="column8"><input value="65" type="text"></td>
<td class="field" id="column9"><input value="0.55" type="text"></td>
</tr>
</table>

4 个答案:

答案 0 :(得分:0)

您可以先循环列,然后循环内部元素

jQuery("[id^=column]").each(function(){
   valid = 0;
   jQuery(this).find("input[type=text]").each(function(){
      if(jQuery(this).val() != "") valid+=1;
   });

  if(valid ==0){
    jQuery(this).hide();
  }
});

答案 1 :(得分:0)

var valid = new array();
$('[id^="column"]').each(function() {
    if ($(this).find('input:text').val().length !== 0) {
        valid[$(this).index()] = 1;
    } else {
        valid[$(this).index()] = 0;
        $(this).hide();
    }
});

您需要创建一个valid的数组,然后您可以逐步浏览列,为每个列设置一个有效的指示符,同时隐藏空白。

答案 2 :(得分:0)

为所有TD提供课程column,然后:

$("td.column").each(function() {
    var valid = false;
    $(this).find("input[type=text]").each(function() {
        if(jQuery(this).val() != "") {
            valid = true;
            return false; // end the .each immediately
        }
    });
    if (!valid) {
        $(this).hide();
    }
});

答案 3 :(得分:0)

由于答案日很短:

$('[id^="column"]:not(:has(input:text:not([value=""])))').hide();