Use jquery to dynamically Add rows to a table

Source: Internet
Author: User

Here, jquery is used. Key Code As follows: Copy code The Code is as follows: // Add a data row;
Function addrow (){
VaR VTB = $ ("# tbdata"); // obtain the jquery object of table id = tbdata.
// All data rows have A. caserow class to get the size of the Data row
VaR vnum = $ ("# tbdata TR"). Filter (". caserow"). Size () + 1; // number of data rows in the table
VaR VTR = $ ("# tbdata # trdatarow1"); // obtain the first row of data in the table.
VaR vtrclone = VTR. Clone (true); // create the first row of the copy object vtrclone
Vtrclone [0]. id = "trdatarow" + vnum; // set the first ID as the index of the current region; prevent duplicate rows with multiple IDS as trdatarow1; add one at a time;
Vtrclone. appendto (VTB); // Add the copy Cell Object to the bottom of the table.
}

This method mainly uses the jquery clone function to clone a row copy of a table. Then add it to the original table.
key code of the deletion method: copy Code the code is as follows: var vnum =$ (" # tbdata TR "). filter (". caserow "). size () + 1; // number of data rows in the table;
If (vnum <= 2)
{< br> alert ('Leave at least one line');
return;
}< br> var vbtndel =$ (this ); // obtain the clicked Button Object
var VTR = vbtndel. parent ("TD "). parent ("TR"); // get the parent tr object;
If (VTR. ATTR ("ID") = "trdatarow1")
{< br> alert ('the first line cannot be deleted! '); // The first row is the basis for cloning and cannot be deleted
return;
}else {
Vtr. remove ();
}

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.