Use jquery to add a row to a specified table dynamically and delete a row

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/linjiqin/p/3148181.html

$ ("#mytable tr"). Find ("Td:nth-child (1)") 1 means get the first column of each row
$ ("#mytable tr"). Find ("Td:nth-child (3)") 3 means get the third column of each row

Today in the project, just use to add a row to the specified table, delete a row, directly to Google, the search out of things is not satisfactory, not the function is not good is the same, it is a waste of time not to please, and so on their own hands encapsulated, and now the code to share out, to avoid repeating the wheel, If you have any questions, please do not make any personal attacks, quack ...

Demand Scenarios

1), add a row

Support to add a row and configurable on any row, such as can add a row in the first row, add a row in the second row, the first line to add a row, the penultimate row to add a row, with the change in demand will not affect.

Prerequisites: The number of rows needs to exist in the table or the add is unsuccessful.

2), delete a row

Supports dynamic deletion of a row.

Show the results first, as you would like to study the code well.

1. The original interface is as follows:

2, add a row, if you want to add a line to click the "Add" button, now click Two will automatically add two lines, the effect is as follows:

3, delete a row, if you want to delete the specified line, first select the row and then click on the "Delete" button to delete (this is not nonsense ~ ~ ~ ~), now select a row, the effect is as follows:

4, delete the selected row, now click the "delete" button will delete the selected row, the effect is as follows:

The code is as follows:

<%@ page language= "java" pageencoding= "UTF-8"%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >   <title> use jquery to add a row to a specified table, delete a row </title>  <meta http-equiv= "Pragma" content= "No-cache" >  <meta http-equiv= "Cache-control" content= "No-cache" >  <meta http-equiv= "Expires" content= "0" >  <meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3" >  <meta http-equiv= "description" content= "This is my page" >  <script type= "Text/javascript"src= "<%=request.getcontextpath ()%>/js/jquery-1.5.1.js" ></script>  <script type= "Text/javascript" >  ////////Add a row, delete a line encapsulation method////////*** Add a row for table specified rows * * Tab Table ID * Row number of rows, such as:0-> first row 1-> second row -2-> penultimate row -1-> last row * trhtml add HTML code for line * */function addtr (tab, Row, trhtml) {//gets the last row of the table $ ("#tab tr:last")//gets the first row of table $ ("#tab tr"). EQ (0)//gets the second last 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); } function deltr (ckb) {//Gets the selected check box, and then loops through the deletevar ckbs=$ ("Input[name=" +ckb+ "]:checked"); if(Ckbs.size () ==0) {alert ("To delete a specified line, select the row you want to delete!" "); return; } Ckbs.each (function () {$ ( This). Parent (). Parent (). remove ();  }); }     /*** Select all * * ALLCKB Select all check box ID * The name of the Items check box*/function Allcheck (allckb, items) {$ ("#"+allckb). Click (function () {$ (' [name= ' +items+ ']:checkbox '). attr ("Checked", This. Checked);  }); }     ////////Add a row, delete a row of test methods///////$ (function () {//Select AllAllcheck ("allckb", "CKB");     }); function addTr2 (tab, row) {var trhtml= "<tr align= ' center ' ><td width= ' 30% ' ><input type= ' checkbox ' name= ' ckb '/></td><td width= ' 30% ' > Geography </td><td width= ' 30% ' >60</td></tr> ';  ADDTR (tab, Row, trhtml); } function DelTr2 () {deltr (' CKB '); }  </script>   <body>  <table border= "1px #ooo" id= "tab" cellpadding= "0"cellspacing= "0" width= "30%" >   &LT;TR align= "center" >       &LT;TD width= "30%" ><input id= "allckb" type= "checkbox"/></td>    &LT;TD width= "30%" > Subjects </td>    &LT;TD width= "30%" > Results </td>   </tr>   &LT;TR align= "center" >       &LT;TD width= "30%" ></td>    &LT;TD width= "30%" > Chinese </td>    &LT;TD width= "30%" >80</td>    </tr>  </table>  <input type= "button" onclick= "ADDTR2 (' tab ',-1) ' value= ' Add >     <input type= "button" onclick= "DelTr2 ()" value= "Delete" >  </body>

Use jquery to add a row to a specified table dynamically and delete a row

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.