开发者

clone a table row

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜