2011-03-10 41 views
0

我希望有人能帮我发现我的代码中的错误。我想要做的只是计算最终用户添加到表中的表行数,并且如果表行不等于2,则会弹出警告框。jQuery计数添加的表行数

这是我的html代码:

<table width="100%" border="0" cellspacing="0" cellpadding="0" id="details"> 
<tr> 
<td colspan="7"><input name="addRow" type="button" class="add" value="Click Here to Add" id="addRow"> 
</td> 
</tr> 
<tr> 
<td><input name="deleteRowButton" type="button" class="deleteRowButton" value="-" id="deleteRowButton" style="margin-top:15px;"> 
</td> 
<td width="" align="left"> 
<select class="section" name="section" style="margin-top:15px;"> 
<option value="select">Select</option> 
</select> 
</td> 
<td width="" align="left">  
<select> </select> 
</td> 
<td width="" align="left">     
<select> </select> 
</td> 
<td width="" align="left">     
<select> </select> 
</td> 
<td width="" align="left"> 
<select>  </select> 
<input type="text" value="" class="text" name="text" style="width: 100px;" /> 
</td> 
<td width="" align="left"> 
<input type="text" /></td> 
</tr> 
</table> 

这里是添加/删除功能:

$("#addRow").live("click", function() { 

     var row = $('#details tbody>tr:last').clone(true); 
     context = $(this).parents("table").filter("#area"); 
     $("td input:text", row).val(""); 
     $("select option:selected", row).attr("selected", false); 
     $("#details", context).append(row); 
    }); 




$('.deleteRowButton').click(DeleteRow); 
     var rowCount = $('#details tr').length;   
     function DeleteRow()  {  
     if (rowCount == 2){ 
      alert($("#details tr").length);   
     } else {  
     $(this).parents('tr').first().remove();  
     } 
     } 

任何人都可以指出的问题给我吗?

+0

请发布html或使用jsFiddle – CoolEsh 2011-03-10 18:20:37

+0

@CoolEsh,我们是否可以看到标记以及添加行函数而不仅仅是删除? – 2011-03-10 18:21:37

+0

我根据您的要求修改了上面的代码 – wpbw 2011-03-10 18:43:03

回答

1

你计算行的行(var rowCount = $('#details tr').length)应该位于DeleteRow()函数内部。

0

它应该是:

if (rowCount != 2){ alert($("#details tr").length);
} else {
$(this).parents('tr').first().remove();
}