


Using jQuery to add and delete the input box group input-group based on Bootstrap_javascript skills
This article shares an example of using jQuery to implement the adding and deleting operations of the input box group input-group for your reference. The specific content is as follows
Note that this requires the use of the input box group of the Bootstrap framework, such as:
<div class="row"> <div class="col-lg-6"> <div class="input-group"> <span class="input-group-addon"> <input type="checkbox" aria-label="..."> </span> <input type="text" class="form-control" aria-label="..."> </div><!-- /input-group --> </div><!-- /.col-lg-6 --> <div class="col-lg-6"> <div class="input-group"> <span class="input-group-addon"> <input type="radio" aria-label="..."> </span> <input type="text" class="form-control" aria-label="..."> </div><!-- /input-group --> </div><!-- /.col-lg-6 --> </div><!-- /.row -->
Rendering of Demo case:
Here provides the self-made plug-in inputGroup.js
The parameter can set whether the middle control in the input box group is a text field or an input box; and set the content of the operation on the right side of the input box group.
To use inputGroup.js, just add a selector in the corresponding container, such as a div, 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 初始化设置 var c = $.extend({ 'widget' : 'input', 'add' : "<span class=\"glyphicon glyphicon-plus\"></span>", 'del' : "<span class=\"glyphicon glyphicon-minus\"></span>" }, options); var _this = $(this); //添加序号为1的输入框组 addInputGroup(1); /** * 添加序号为order的输入框组 * @param order 输入框组的序号 */ function addInputGroup(order) { //1.创建输入框组 var inputGroup = $("<div class='input-group' style='margin: 10px 0'></div>"); //2.输入框组的序号 var inputGroupAddon1 = $("<span class='input-group-addon'></span>"); //3.设置输入框组的序号 inputGroupAddon1.html(" " + order + " "); //4.创建输入框组中的输入控件(input或textarea) var widget = '', inputGroupAddon2; if(c.widget == 'textarea') { widget = $("<textarea class='form-control' style='resize: vertical;'></textarea>"); inputGroupAddon2 = $("<span class='input-group-addon'></span>"); } else if(c.widget == 'input') { widget = $("<input class='form-control' type='text'/>"); inputGroupAddon2 = $("<span class='input-group-btn'></span>"); } //5.创建输入框组中最后面的操作按钮 var addBtn = $("<button class='btn btn-default' type='button'>" + c.add + "</button>"); addBtn.appendTo(inputGroupAddon2).on('click', function() { //6.响应删除和添加操作按钮事件 if($(this).html() == c.del) { $(this).parents('.input-group').remove(); } else if($(this).html() == c.add) { $(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").eq(0).html(' ' + (i + 1) + ' '); }); } } });
Demo case——InputGroupDemo
The directory structure is as follows:
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>输入框组</title> <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"> <style> /*.input-group-add .input-group { margin: 10px 0; }*/ </style> <!--<link href="assets/font-awesome/css/font-awesome.css" rel="stylesheet" />--> <!--[if lt IE 9]> <script src="js/html5shiv.js"></script> <script src="js/respond.min.js"></script> <![endif]--> </head> <body> <div class="container"> <div class="input-group-add"> <!--<div class="input-group"> <span class="input-group-addon"> 1 </span> <!–<input type="text" class="form-control" aria-label="...">–> <textarea class="form-control"></textarea> <span class="input-group-addon"> <button class="btn btn-default" type="button"> + </button> </span> </div>--> </div> </div> <script src="js/jquery-1.11.1.min.js"></script> <script src="js/bootstrap.min.js"></script> <script src="inputGroup.js"></script> <script> $(function() { $('.input-group-add').initInputGroup({ 'widget' : 'textarea', //输入框组中间的空间类型 /*'add' : '添加', 'del' : '删除'*/ }); }); </script> </body> </html>
If the middle control in the input box group requires input, you can set:
$('.input-group-add').initInputGroup({ 'widget' : 'input', //输入框组中间的空间类型 /*'add' : '添加', 'del' : '删除'*/ });
Or do not set it, because the default middle control is input.
The effect of the middle control being input is as follows:
The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use Bootstrap to get the value of the search bar: Determines the ID or name of the search bar. Use JavaScript to get DOM elements. Gets the value of the element. Perform the required actions.

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

Use Bootstrap to implement vertical centering: flexbox method: Use the d-flex, justify-content-center, and align-items-center classes to place elements in the flexbox container. align-items-center class method: For browsers that do not support flexbox, use the align-items-center class, provided that the parent element has a defined height.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-
