本文實例為大家分享使用jQuery實現輸入框組input-group的添加與刪除操作,供大家參考,具體內容如下
注意這里要求使用到Bootstrap框架的輸入框組,如:
<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 -->
Demo案例的效果圖:
這里提供自制的 插件 inputGroup.js
參數為可以設置 輸入框組中中間的控件是文本域還是輸入框;以及設置在輸入框組右側的操作的內容。
使用inputGroup.js只要在對應的容器,如div中添加選擇器,然后使用jQuery獲取該選擇器對應的jQuery對象,調用 initInputGroup方法即可。
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案例――InputGroupDemo
目錄結構如下:
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> <! 主站蜘蛛池模板: 湘乡市| 玉门市| 衡阳县| 黑河市| 五常市| 沙田区| 肥东县| 监利县| 福贡县| 东宁县| 方正县| 宁武县| 肥城市| 孝昌县| 永定县| 新昌县| 灵宝市| 营口市| 静宁县| 景泰县| 东丽区| 吴川市| 花莲市| 儋州市| 石嘴山市| 肇庆市| 梧州市| 五常市| 伊春市| 塔河县| 延庆县| 买车| 景东| 阿克陶县| 宝清县| 珠海市| 恩施市| 旬阳县| 洮南市| 泗水县| 于都县|