基于Bootstrap使用jQuery实现输入框组input-group的添加与删除-改进版
- /**
- * Created by DreamBoy on 2016/6/4.
- */
- /**
- * 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>",
- field: '',
- data: []
- }, options);
- var _this = $(this);
- _this.children().remove();
- //添加序号为1的输入框组
- addInputGroup(1);
- /**
- * 添加序号为order的输入框组
- * @param order 输入框组的序号
- * @param data 初始化输入框组中的数据
- */
- 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>");
- widget.html(c.data[order - 1]);
- inputGroupAddon2 = $("<span class='input-group-addon'></span>");
- } else if(c.widget == 'input') {
- widget = $("<input class='form-control' type='text'/>");
- widget.val(c.data[order - 1]);
- inputGroupAddon2 = $("<span class='input-group-btn'></span>");
- }
- //5.设置表单提交时的字段名
- if(c.field.length == 0) {
- widget.prop('name', c.widget + 'Data[]');
- } else {
- widget.prop('name', c.field + '[]');
- }
- //6.创建输入框组中最后面的操作按钮
- var addBtn = $("<button class='btn btn-default' type='button'>" + c.add + "</button>");
- addBtn.appendTo(inputGroupAddon2).on('click', function() {
- //7.响应删除和添加操作按钮事件
- if($(this).html() == c.del) {
- $(this).parents('.input-group').remove();
- } else if($(this).html() == c.add) {
- $(this).html(c.del);
- addInputGroup(order+1);
- }
- //8.重新排序输入框组的序号
- resort();
- });
- inputGroup.append(inputGroupAddon1).append(widget).append(inputGroupAddon2);
- _this.append(inputGroup);
- if(order + 1 > c.data.length) {
- return;
- }
- addBtn.trigger('click');
- }
- function resort() {
- var child = _this.children();
- $.each(child, function(i) {
- $(this).find(".input-group-addon").eq(0).html(' ' + (i + 1) + ' ');
- });
- }
- }
- });
- <!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">
- <!--<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>
- </div>
- <script src="js/jquery-1.11.1.min.js"></script>
- <script src="js/bootstrap.min.js"></script>
- <script src="inputGroup-1.1.js"></script>
- <script>
- $(function() {
- $('.input-group-add').initInputGroup({
- 'widget' : 'input', //输入框组中间的空间类型
- /*'add' : '添加',
- 'del' : '删除'*/
- 'field': 'data',
- 'data' : ['haha', 'hello', 'hi', 'dj']
- });
- });
- </script>
- </body>
- </html>
基于Bootstrap使用jQuery实现输入框组input-group的添加与删除-改进版的更多相关文章
- Bootstrap<基础十五> 输入框组
Bootstrap 支持的另一个特性,输入框组.输入框组扩展自 表单控件.使用输入框组,可以很容易地向基于文本的输入框添加作为前缀和后缀的文本或按钮. 通过向输入域添加前缀和后缀的内容,您可以向用户输 ...
- Bootstrap历练实例:输入框组的大小
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 基于Bootstrap的JQuery TreeView树形控件,数据支持json字符串、list集合(MVC5)<二>
上篇博客给大家介绍了基于Bootstrap的JQuery TreeView树形控件,数据支持json字符串.list集合(MVC5)<一>, 其中的两种方式都显得有些冗余.接着上篇博客继续 ...
- 基于bootstrap的jQuery多级列表树插件 treeview
http://www.cnblogs.com/mfc-itblog/p/5233453.html http://www.htmleaf.com/jQuery/Menu-Navigation/20150 ...
- 基于bootstrap的jQuery多级列表树插件
简要教程 bootstrap-treeview是一款效果非常酷的基于bootstrap的jQuery多级列表树插件.该jQuery插件基于Twitter Bootstrap,以简单和优雅的方式来显示一 ...
- Bootstrap 基于Bootstrap和JQuery实现动态打开和关闭tab页
基于Bootstrap和JQuery实现动态打开和关闭tab页 by:授客 QQ:1033553122 1. 测试环境 JQuery-3.2.1.min.j Bootstrap-3.3.7-d ...
- 基于Bootstrap的jQuery开关按钮组合
Bootstrap是一款由Twitter推出的开源前端开发包,功能非常强大.今天我们要分享的这款jQuery开关按钮组合就是基于Bootstrap框架的,看了按钮的主题样式,也都是Bootstrap的 ...
- 从零开始学 Web 之 jQuery(四)元素的创建添加与删除,自定义属性
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表单:输入框(Input)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
随机推荐
- node-inspector调试报错问题处理
使用node-inspector调试的时候,提示下面的异常,我的node版本是6.9.2出现下面的异常,我同事的版本4.2.1就没有这个问题. C:\Users\dzm>node-inspect ...
- 每日英语:Can Going In and Out of Air Conditioning Cause Colds?
For most people, summer involves numerous daily shifts between scorching outdoor heat and frosty air ...
- ny49 开心的小明
开心的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 小明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他 ...
- cvs history,CVS中查询目录下所有文件的提交记录
我们习惯用eclipse svn中查看一个目录下,甚至整个工程下,所有的文件的变更列表:操作为:右键工程 -> Team -> Show History. 最近在使用CVS,eclipse ...
- 09、win32 转换为 store app
1.机制: 微软的 Project Centernial ( Project C) 项目的就是把传统的 windows桌面应用程序 转换为 windows10 store app (appx). 目的 ...
- 【转】linux configure报错configure: error: C++ preprocessor “/lib/cpp” fails sanity 的解决办法
/lib/cpp fails sanity check的解决 在某些软件的时候,运行./configure 会报错,错误提示为: configure: error: C++ preprocessor ...
- struts2文件下载的编写步骤(文件导出)和输入流转换的方法
strut2文件下载三部曲:一个流.两个头信息 说明: ①一个流,在Action中需要一个getInputStream()的方法来获取下载的内容,其中inputStream是默认的,他会指示Strea ...
- MySQL Daemon failed to start
http://blog.163.com/cmdbat@126/blog/static/17029212320122804743900/
- VBA学习笔记(3)--文件夹操作
说明(2017.3.22): 1. 根据兰色幻想VBA80集视频教学,总结 2. 大部分可以用自带函数处理,不过复制文件夹需要用到FileSystemObject对象,打开文件夹用到shell Pub ...
- 各种hash函数
public class HashAlgorithms { /** * 加法hash * @param key 字符串 * @param prime 一个质数 * @return hash结果 */ ...