jquery method adds a Delete row to a table dynamically

Source: Internet
Author: User

1. Increase the line

For example, set table ID to tab var trhtml = "<tr><td>...</td></tr>"
$ ("#tab"). Append (trhtml);//Add a row to the last face of the table
$ ("#tab Tr:eq (2)"). After (trhtml); Add a row after the 3rd row of the table
This can be dynamically added to the line, as to what you are the event to dynamically add that depends on your own meaning, through the button or div and other click events to add, just put the above two lines of code in the OK, note, to put the Var trhtml that line of code into the Add event, Otherwise, no matter how many clicks, you can add only one row
$ (function () {
$ (": Button"). Click (function () {
var tr = "<tr><td>new</td></tr>";
$ ("table"). Append (tr);
$ ("Table Tr:eq (2)"). After (TR);
});
});

This is the code that I tested with, you can run to see

2. jquery deletes the specified row in table

The number of rows is click the Delete button to pass through the master to help answer

1 $ (' Table Tr:eq (1) '). Remove ();
Ask:
My number of rows is the click button to pass the past should write
Chase Answer:
How do you pass the code?
Ask:
<input type= "button" value= "x" onclick= "DELETETR (1)"/>
Chase Answer:
function Deletetr (i) {
   $ (' Table Tr:eq (' + i + ') '). Remove ();

Ask:
No  , I didn't respond when I clicked the Delete button.
Chase Answer:
Yes, you can see what's wrong.
Ask:
I changed to $ (' #table tr:eq (' + i + ') '). Remove (), but Delete is always not the number of rows I passed the output of I would not be 0 if not 1 What's going on?
Chase Answer:
Do you have a lot of buttons, like when I delete the second line, the third line becomes the second line?
Ask:

I have a lot of buttons on a page. But I only have one button in my table. Answer:

<input type= "button" value= "x" onclick= "Deletetr (This)"/>

function deletetr (obj) {
       $ (obj). Parent (' TR ') ). Remove ();
Ask:
Oh, yes
Chase Answer:
Try the above.
Ask:
No, no,  click the Delete button.
Chase Answer:
function Deletetr (obj) {
  $ (obj). Closest (' tr '). Remove ();
Questioner evaluation
Thank you!


3. Get table row and column Count

function init ()
{
var table =document.getelementbyid ("Tab1");
var rows = table.rows.length;
Alert (' Number of rows ' +rows);
var colums = table.rows[0].cells.length;
Alert (' Number of columns ' +colums);
}

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.