Jquery _ jquery

Source: Internet
Author: User
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.

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.