This article describes how to add and delete an input-group using jQuery Based on Bootstrap, for more information, see the examples in this article.
Note that you must use the input box Group of the Bootstrap framework, for example:
Demo case:
The self-made plugin inputGroup. js is provided here.
The parameter indicates whether the control in the input box group is a text field or an input box, and the operation content on the right of the input box group.
To use inputGroup. js, you only need to add a selector to the corresponding container, for example, p. Then, use jQuery to obtain the jQuery object corresponding to the selector and call the initInputGroup method.
InputGroup. js
/*** Created by DreamBoy on 2016/4/29. */$ (function () {$. fn. initInputGroup = function (options) {// 1. settings initialization Settings var c =$. extend ({'widget ': 'input', 'add': "", 'del': ""}, options); var _ this = $ (this ); // Add the addInputGroup (1) to the input box group with the serial number of 1;/*** Add the serial number of the input box group with the serial number of order * @ param order input box group number */function addInputGroup (order) {// 1. create input box group var inputGroup = $ (""); // 2. input box Group No. var inputGroupAddon1 = $ (""); // 3. set the serial number of the input box group inputGroupAddon1.html ("" + order + ""); // 4. create input controls (input or textarea) var widget = '', inputGroupAddon2; if (c. widget = 'textea ') {widget = $ (""); InputGroupAddon2 = $ (" ");} else if (c. widget = 'input') {widget = $ (""); InputGroupAddon2 =2 (" ") ;}// 5. Create the last operation button in the input box group var addBtn =$ (""+ C. add +""); AddBtn. appendTo (inputGroupAddon2 ). on ('click', function () {// 6. response to the delete and add OPERATION button event if({(this}.html () = c. del) {$ (this ). parents ('. input-group '). remove ();} else if(%(this%.html () = c. add) {signature (this).html (c. del); addInputGroup (order + 1);} // 7. resort () ;}); inputGroup. append (inputGroupAddon1 ). append (widget ). append (inputGroupAddon2); _ this. append (inputGroup);} function resort () {var child = _ this. children (); $. each (child, function (I) {$ (this ). find (". input-group-addon "2.16.eq(02.16.html ('' + (I + 1) + '');});}}});
Demo case -- InputGroupDemo
The directory structure is as follows:
Index.html
Input box Group