Jquery dynamic addition/deletion plug-in

Source: Internet
Author: User

[Javascript]
// Dynamic content area JS by dust margin 2012 qq: 80213876
 
(Function ($ ){
Var defaults = {
Container: ". dynamicCon", // The outermost container class
Temp: ". temp", // template layer class
Items: ". items", // class
MinCount: 1, // the minimum number of rows (there is a hidden template row at the initial stage. + 1 is required for specific calculation)
MaxCount: 50, // The maximum number of specific rows (there is a hidden template row at the initial stage. + 1 is required for specific calculation)
AddBtn: ". addBtn", // Add button class
DelBtn: ". delBtn" // Delete the button class
}
$. Extend ({
DynamicCon: function (options ){
Options = $. extend (defaults, options );
$ Con = $ (options. container );
$ Temp = $ con. find (options. temp); // template line
$ Temp. attr ({"class": options. items. substring (1, options. items. length )}). wrap ("<div style = 'display: none'> </div> ");
Var tempHtml = escape(%temp.parent(%.html ());
$ Temp. hide ();
// Add a row event
$ Con. find (options. addBtn). live ("click", function (){
If ($ con. find (options. items). length = options. maxCount + 1 ){
Alert ("up to" + options. maxCount + "lines can be added! "); Return false;
}
$ (This). closest (options. items). after (unescape (tempHtml ));
});
// Delete a row event
$ Con. find (options. delBtn). live ("click", function (){
If ($ con. find (options. items). length = options. minCount + 1 ){
Alert ("at least" + options. minCount + "line! "); Return false;
}
$ (This). closest (options. items). remove ();
});
}
});
}) (JQuery );

Demo:
[Html]
<Table style = "width: 100%; border: solid 1px # 78b0dc;" class = "dynamicCon">
<Tr>
<Td colspan = "3" align = "left" style = "color: # f00;">
Note: The field name can only be a string consisting of Chinese characters, numbers, letters, or underscores. The weight can only be non-negative! When the field name is blank, it will not be added! If the weight is null, the default value is 0. You do not need to enter the weight information for the simple edition !!
</Td>
</Tr>
<Tr>
<Td align = "center" width = "40%"> Field name </td>
<Td align = "center" width = "40%" class = "trNoSimple"> weight (Single Point) </td>
<Td align = "center" width = "20%"> operation </td>
</Tr>
<Tr class = "items">
<Td align = "center">
<Input type = "text" name = "txtFieldName" value = "<% # Eval (" FieldRealName ") %>"/>
<Input type = "hidden" name = "txtFieldId" value = "<% # Eval (" Id ") %>"/>
</Td>
<Td class = "trNoSimple">
<Input type = "text" name = "txtFieldPoint" value = "<% # Eval (" Point ") %>"/>
</Td>
<Td align = "center"> <a href = "javascript: void (0);" class = "addBtn"> Add </a> <a href = "javascript: void (0); "class =" delBtn "> Delete </a> </td>
</Tr>
<Tr class = "temp">
<Td align = "center"> <input type = "text" name = "txtFieldName"/> </td>
<Td class = "trNoSimple"> <input type = "text" name = "txtFieldPoint" value = "0"/> </td>
<Td align = "center"> <a href = "javascript: void (0);" class = "addBtn"> Add </a> <a href = "javascript: void (0); "class =" delBtn "> Delete </a> </td>
</Tr>
</Table>

$. DynamicCon ({maxCount: 25 });


From BLOG

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.