Jquery dynamically adds and deletes the control instance code

Source: Internet
Author: User

You can add custom parameters and variables required during the increase or decrease process, such as $ ("table"). dynamicaddremover ({param1: "param1", param2: "param2 "});
For example, options. param1.
. The root container must have a unique id and must be unique at the specified time. If multiple modules (multiple root containers) need to be used: $ ("# table1 "). dynamicaddremover (), $ ("# table2 "). dynamicaddremover () and so on.
The id and name in the newly added control are not updated. If you have special business needs, add the business logic for generating the new id in addedevent.

Instance 1

<? Xml version = "1.0" encoding = "iso-8859-1"?>
<Html>
<Head>
<Title> </title>
<Script language = "webpage effects" src = "jquery-1.4.2.min. webpage effects"> </script>
<Script language = "javascript" src = "dynamicaddremover. js"> </script>
<Script language = "javascript">

$ (Function (){
$ ("# Table1"). dynamicaddremover ({
Addedevent: function (newctl, options ){
// Clear the textbox.
Newctl. find (": text"). val ('');
If ($ ("table"). size ()> 5)
{
// Hide the add button.
Newctl. find ("img [id $ = '" + options. adderid + "']"). hide ();
}
},
Removedevent: function (prevctl, nextctl, options ){
If ($ ("table"). size () <= 5)
{
// Show the add button.
$ ("Table: last"). find ("img [id $ = '" + options. adderid + "']"). show ();
}
}
});
});
</Script>
</Head>
<Body>
<Table id = 'table1'>
<Tr>
<Td>
<Select>
<Option value = "volvo"> volvo </option>
<Option value = "saab"> saab </option>
<Option value = "opel"> opel </option>
<Option value = "audi"> audi </option>
</Select>
<Input type = "text" name = "text1" id = "text1" value = "text"> </input>
<Button type = "button" name = "btn1" value = "btn1" id = "btn1"> button </button>
</Td>
</Tr>
</Table>
</Body>
</Html>

Instance 2

<? Xml version = "1.0" encoding = "iso-8859-1"?>
<Html>
<Head>
<Title> </title>
<Script language = "javascript" src = "jquery-1.4.2.min.js"> </script>
<Script language = "javascript" src = "dynamicaddremover. js"> </script>
<Script language = "javascript">
$ (Function (){
$ ("Table"). dynamicaddremover ();
});
</Script>
</Head>
<Body>
<Table>
<Tr>
<Td>
<Select>
<Option value = "volvo"> volvo </option>
<Option value = "saab"> saab </option>
<Option value = "opel"> opel </option>
<Option value = "audi"> audi </option>
</Select>
<Input type = "text" name = "text1" id = "text1" value = "text"> </input>
<Button type = "button" name = "btn1" value = "btn1" id = "btn1"> button </button>
</Td>
</Tr>
</Table>
</Body>
</Html>
Add controls, such as the file upload box and the input box, and use the two examples above for instance. However, the instance is an unlimited one that can control the number of controls added, and the deletion control is added.

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.