Home > Web Front-end > JS Tutorial > Using jQuery to add and delete the input box group input-group based on Bootstrap_javascript skills

Using jQuery to add and delete the input box group input-group based on Bootstrap_javascript skills

WBOY
Release: 2016-05-16 15:02:32
Original
1948 people have browsed it

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 --> 
Copy after login

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) + ' '); 
      }); 
    } 
  } 
}); 
Copy after login

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> 
Copy after login

If the middle control in the input box group requires input, you can set:

$('.input-group-add').initInputGroup({ 
        'widget' : 'input', //输入框组中间的空间类型 
        /*'add' : '添加', 
        'del' : '删除'*/ 
      }); 
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template