2011-03-14 152 views
1

您好我有两行 像:克隆表行

<tr> 
    <td> 
    <%:Html.Label("Name")%> 
    <%:Html.TextBoxFor(model=>model.Name)%> 
    </td> 
</tr> 
<tr> 
    <td> 
    <%:Html.Label("Age")%> 
    <%:Html.TextBoxFor(model=>model.Age)%> 
    </td> 
</tr> 

我应该如何克隆这些两行和最后一行完成后添加。 我想是这样的:

$("#" + tableId + "tbody") 
    .clone(true) 
    .insertAfter("#" + tableId + " tr:last"); 
$('#' + tableId + ' tbody:first>tr:last>td:last') 
    .empty() 
    .append("<input type=\"image\" id=\"imgDelete\" name=\"delete\" alt='' class=\"delete\" onclick='DeleteTableRow(this)' />"); 
+1

哇请编辑这使它有点可读... – Matt 2011-03-14 12:15:12

+0

你想在服务器端或客户端添加行吗?你要做的是在客户端使用jQuery。如果服务器端可以使用asp.net mvc helper。 – CallMeLaNN 2011-03-14 18:48:35

回答

0

我只想克隆2行,你可以给他们和id,例如:

<tr id="toClone_0"> 
    <td> 
    <%:Html.Label("Name")%> 
    <%:Html.TextBoxFor(model=>model.Name)%> 
    </td> 
</tr> 
<tr id="toClone_1"> 
    <td> 
    <%:Html.Label("Age")%> 
    <%:Html.TextBoxFor(model=>model.Age)%> 
    </td> 
</tr> 

然后在jQuery的:

// select the elements whose id starts with 'toClone_' 
// false to avoid cloning of event handlers, true ohterwise 
var clonedRows = $("id^='toClone_'").clone(false); 

// insert the cloned rows after the last row 
$("#" + tableId + "tr:last").after(clonedRows);