jquery dynamically adding and deleting table rows _jquery

Source: Internet
Author: User
Tags rowcount

This example describes how jquery dynamically adds and deletes table rows. Share to everyone for your reference. The specific analysis is as follows:

Yesterday do page table row dynamic Add and delete, read countless introduction, found a good dongdong, JQuery. It is really very convenient to implement it, this is a way I use our platform.

Copy Code code as follows:
Record Add row Count
var areacount=1;
Record number of actual table rows
var rowcount=1;
Delete template html
var delrowtemplete = "<td><a href= ' javascript:void (0); ' class= ' Content_del ' onclick= ' (This) ' > Delete </a></td> ";
Table row Template
var addrowtemplete= "";
$ (function () {
First remove the template that needs to be cloned, and the template line <tr>id to Rowtemplete_0
Addrowtemplete= $ ("#rowTemplete_0"). html ();
});
Increase row
function Addbatchrow (type) {
var templete = $ ("<tr id= ' Rowtemplete_" +areacount+ "' ></tr>");
Increment sequence number, replace TR or TD exist [0], _0 or (0) to prevent the same ID, so the design is mainly for the background value of convenience
Templete = Templete.append (Addrowtemplete.replace (/\[0\]/g, "[" +areacount+ "]"). Replace (/_0/g, "_" +areacount).
Replace ("Processstat" ("0\") "," Processstat ("+areacount+")));
Find the last line that exists, and then stitch a line after it
var flag = false;
for (Var i=areacount-1;i>=0;i--) {
if ($ ("#rowTemplete_" +i). Length>0) {$ ("#rowTemplete_" +i). After (Templete.append (Delrowtemplete));
break;
}
Count Plus One
areacount++; rowcount++;
}
Delete Row
function Deletebatchrow (obj) {
if (rowcount>1) {
$ (obj). Parents ("tr"). Remove ();
rowcount--;
else alert ("Keep at least one row");//If all deleted, then there is no way to add lines, template lines have been deleted AH
}
Get the number of bars
function Getareacount () {
return rowcount;
}

The background is also relatively easy to use, in Formbean inside the definition of array variables to get and set, very convenient.

I hope this article will help you with your jquery programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.