2011-03-14 159 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);