簡體   English   中英

jQuery計數添加的表行數

[英]jQuery count number of added table rows

我希望有人可以幫助我發現代碼中的錯誤。 我想做的只是簡單地計算最終用戶添加到表中的表行數,如果表行數不等於2,則會彈出一個警告框。

這是我的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();      
       } 
       }

誰能向我指出問題?

計算行的行(var rowCount = $('#details tr')。length)應該在DeleteRow()函數內部。

應該是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM