This article mainly introduces jquery's method of adding and deleting rows. it involves the addition and deletion techniques of table row and has some reference value, for more information about how to add and delete rows using jquery, see the following example. Share it with you for your reference. The specific analysis is as follows:
Recently, a voting management module needs to add question options. for convenience, you can simply add and delete table rows.
Note: jquery. js needs to be introduced.
First: (There are four rows in form by default)
Form code:
The code is as follows:
Problem (Single choice) (Check) :
|
$ {Question}
|
|
| Option 1: |
|
|
| Option 2: |
|
|
| Option 3: |
|
|
| Option 4: |
|
|
Add a row
JS code:
The code is as follows:
Var rowCount = 4; // the default number of rows is 4.
// Add rows
Function addRow (){
RowCount ++;
Var newRow ='Option '+ rowCount + ':Delete';
$ ('# Optioniner INER'). append (newRow );
}
// Delete a row
Function delRow (rowIndex ){
$ ("# Option" + rowIndex). remove ();
RowCount --;
}
Note thatYou need to define the ID. if there is a row by default, the ID is defined regularly as shown in the code, so that you can easily define the new line ID when adding a row.
Define a row number variable in JS, because the default four rows in my form (the first line, that is, id = 'option0' can be left empty ), therefore, the default rowCount defined in JS is 4.
OK. That's simple.
In addition, if you need to add rows at the specified position, you need to write
The code is as follows:
$ ("# Tab tr"). eq (-2). after ("Keyword name:*");
-2 is to add rows after the last and second tr.
Tab is the table id.
I hope this article will help you with jQuery programming.