jquery Dynamic Add a simple instance of the delete row (recommended) _jquery

Source: Internet
Author: User
Tags first row

Recently write program, happen to have dynamic add delete line, the following is recorded

HTML is not written, there is nothing, directly on the core of the

New Line

function AddRow (obj) {
//Get a total number of rows in the table, convenient to append to the ordinal number assigned
  var length = $ ("#grid tr"). Length;
To get the current number of lines, so that when you append, add under the line, I have more levels across, define var current = $ (obj) based on the actual level. Parent (). Parent ().
  parent (). Prevall (). length;   alert (current);
Here the main definition needs to append content, of course, starting from TR, very simple, everyone will
  var addcontent = "";
The core method, Gird is the table id,current is inserted after the first few lines, Addcontent is to append the content of the
  addtr (' grid ', current,addcontent);
Here's the reordering, reordering the numbers from 1 to N, and it happens that the first TR in my code doesn't show up, so it's not + 1
  $ ("#grid tr"). each (function (index) {
     if (index!= 0) {
       $ (this). Children (' TD '). EQ (0). text (index);
     }
  );

OK, let's look at the next Addtr method ~ ~ This is found on the Internet, or good use of

/**
 * Adds a row to the table specified row
 *
 tab ID
 * Row number, such as:0-> first line 1-> second row -2-> penultimate line -1-> last line
 * trhtml Add line HTML code
 */
function addtr (tab, Row, trhtml) {
  //Get Table last line $ ("#tab tr:last")
  //Get first row of table $ ("#tab tr"). EQ (0)
  //Get the penultimate row of table $ ("#tab tr"). EQ ( -2)
  var $tr =$ ("#" +tab+ "tr"). EQ (row);
  if ($tr. Size () ==0) {
    alert ("The specified table ID or number of rows does not exist!) ");
    return;
  }
  $tr. After (trhtml);
 }

Delete Row, this is easier

function DeleteRow (obj) {
//Remove $ (obj). Parent (). Parent ().
   parent (). remove ();
Reorder, if the deletion is not the last line, do not reorder can not see the ~
   $ ("#grid tr"). each (function (index) {
     if (index!= 0) {
       $ (this). Children (' TD '). EQ (0). text (index);
     }
   );
   

Dynamic add Delete Row, just a few lines of code, convenient for everyone to use

PS: $ (this). Prevall (). length + 1;//column

The above is the small series for you to bring the jquery dynamic add a simple instance of deleting rows (recommended) All content, I hope that we support the cloud-Habitat Community ~

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.