基于Bootstrap使用jQuery實(shí)現(xiàn)輸入框組input-group的添加與刪除
本文實(shí)例為大家分享使用jQuery實(shí)現(xiàn)輸入框組input-group的添加與刪除操作,供大家參考,具體內(nèi)容如下
注意這里要求使用到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
參數(shù)為可以設(shè)置 輸入框組中中間的控件是文本域還是輸入框;以及設(shè)置在輸入框組右側(cè)的操作的內(nèi)容。
使用inputGroup.js只要在對(duì)應(yīng)的容器,如div中添加選擇器,然后使用jQuery獲取該選擇器對(duì)應(yīng)的jQuery對(duì)象,調(diào)用 initInputGroup方法即可。
inputGroup.js
/** * Created by DreamBoy on 2016/4/29. */ $(function() { $.fn.initInputGroup = function (options) { //1.Settings 初始化設(shè)置 var c = $.extend({ 'widget' : 'input', 'add' : "<span class=\"glyphicon glyphicon-plus\"></span>", 'del' : "<span class=\"glyphicon glyphicon-minus\"></span>" }, options); var _this = $(this); //添加序號(hào)為1的輸入框組 addInputGroup(1); /** * 添加序號(hào)為order的輸入框組 * @param order 輸入框組的序號(hào) */ function addInputGroup(order) { //1.創(chuàng)建輸入框組 var inputGroup = $("<div class='input-group' style='margin: 10px 0'></div>"); //2.輸入框組的序號(hào) var inputGroupAddon1 = $("<span class='input-group-addon'></span>"); //3.設(shè)置輸入框組的序號(hào) inputGroupAddon1.html(" " + order + " "); //4.創(chuàng)建輸入框組中的輸入控件(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.創(chuàng)建輸入框組中最后面的操作按鈕 var addBtn = $("<button class='btn btn-default' type='button'>" + c.add + "</button>"); addBtn.appendTo(inputGroupAddon2).on('click', function() { //6.響應(yīng)刪除和添加操作按鈕事件 if($(this).html() == c.del) { $(this).parents('.input-group').remove(); } else if($(this).html() == c.add) { $(this).html(c.del); addInputGroup(order+1); } //7.重新排序輸入框組的序號(hào) 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
目錄結(jié)構(gòu)如下:
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>
如果輸入框組中的中間控件需要input,則可以設(shè)置:
$('.input-group-add').initInputGroup({ 'widget' : 'input', //輸入框組中間的空間類型 /*'add' : '添加', 'del' : '刪除'*/ });
或者不進(jìn)行設(shè)置,因?yàn)槟J(rèn)中間控件為input。
中間控件為input的效果如下:
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊這里進(jìn)行學(xué)習(xí),再為大家附3個(gè)精彩的專題:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
- jQuery/JS監(jiān)聽(tīng)input輸入框值變化實(shí)例
- jQuery實(shí)現(xiàn)input輸入框獲取焦點(diǎn)與失去焦點(diǎn)時(shí)提示的消失與顯示功能示例
- jQuery實(shí)現(xiàn)動(dòng)態(tài)添加、刪除按鈕及input輸入框的方法
- js與jquery實(shí)時(shí)監(jiān)聽(tīng)輸入框值的oninput與onpropertychange方法
- jquery實(shí)現(xiàn)input輸入框?qū)崟r(shí)輸入觸發(fā)事件代碼
- input 輸入框獲得/失去焦點(diǎn)時(shí)隱藏/顯示文字(jquery版)
- 基于jQuery的input輸入框下拉提示層(自動(dòng)郵箱后綴名)
- input 和 textarea 輸入框最大文字限制的jquery插件
- jQuery 版本的文本輸入框檢查器Input Check
- jquery獲取input輸入框中的值
相關(guān)文章
微信小程序 網(wǎng)絡(luò)通信實(shí)現(xiàn)詳解
這篇文章主要介紹了微信小程序 網(wǎng)絡(luò)通信實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07細(xì)說(shuō)JS數(shù)組遍歷的一些細(xì)節(jié)及實(shí)現(xiàn)
本文主要介紹了細(xì)說(shuō)JS數(shù)組遍歷的一些細(xì)節(jié)及實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05JavaScript利用fetch實(shí)現(xiàn)異步請(qǐng)求的方法實(shí)例
傳遞信息到服務(wù)器,從服務(wù)器獲取信息,是前端發(fā)展的重中之重,尤其是現(xiàn)在前后端分離的大前提下,前后端的數(shù)據(jù)交互是前端的必修科目了,下面這篇文章主要給大家介紹了關(guān)于JavaScript利用fetch實(shí)現(xiàn)異步請(qǐng)求的相關(guān)資料,需要的朋友可以參考借鑒。2017-07-07js實(shí)現(xiàn)自定義滾動(dòng)條的示例
這篇文章主要介紹了js實(shí)現(xiàn)自定義滾動(dòng)條的示例,幫助大家制作JS特效,美化自身網(wǎng)頁(yè),感興趣的朋友可以了解下2020-10-10原生js實(shí)現(xiàn)表單的正則驗(yàn)證(驗(yàn)證通過(guò)后才可提交)
這篇文章主要給大家介紹了關(guān)于如何利用原生js實(shí)現(xiàn)表單的正則驗(yàn)證,所有驗(yàn)證都通過(guò)后提交按鈕才可用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05mysql輸出數(shù)據(jù)賦給js變量報(bào)unterminated string literal錯(cuò)誤原因
mysql 數(shù)據(jù)庫(kù)數(shù)據(jù)賦給js變量報(bào)unterminated string literal錯(cuò)誤原因2010-05-05JavaScript 字符串常用操作小結(jié)(非常實(shí)用)
這篇文章主要介紹了JavaScript 字符串常用操作的知識(shí),包括字符串截取,查找類的方法,對(duì)js字符串操作相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-11-11