开发者

clone a table row

开发者 https://www.devze.com 2023-02-16 13:05 出处:网络
Hi I have two rows like : <tr> <td> <%:Html.Label(\"Name\")%> <%:Html.TextBoxFor(model=>model.Name)%>

Hi I have two rows like :

<tr>
  <td>
    <%:Html.Label("Name")%>
    <%:Html.TextBoxFor(model=>model.Name)%>
  </td>
</tr>
<tr>
  <td>
    <%:Html.Label("Age")%>
    <%:Html.TextBoxFor(mode开发者_如何学Pythonl=>model.Age)%>
  </td>
</tr>

How should I clone these two rows and add it after the completion of last row. I tried something like this:

$("#" + 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)' />");


I you only want to clone the 2 rows, you could give them and id, for instance:

<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>

then in 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);
0

精彩评论

暂无评论...
验证码 换一张
取 消