jQuery實(shí)現(xiàn)動(dòng)態(tài)添加、刪除按鈕及input輸入框的方法
本文實(shí)例講述了jQuery實(shí)現(xiàn)動(dòng)態(tài)添加、刪除按鈕及input輸入框的方法。分享給大家供大家參考,具體如下:
<html>
<head>
<meta charset="utf-8">
<title>動(dòng)態(tài)創(chuàng)建按鈕</title>
<script src='http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js'></script>
</head>
<body>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="AddMoreFileBox" class="btn btn-info">添加更多的input輸入框</a></span></p>
<div id="InputsWrapper">
<div><input type="text" name="mytext[]" id="field_1" value="Text 1"/><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="removeclass"><input type='button' value='刪除'></a></div>
</div>
<script>
$(document).ready(function() {
var MaxInputs = 8; //maximum input boxes allowed
var InputsWrapper = $("#InputsWrapper"); //Input boxes wrapper ID
var AddButton = $("#AddMoreFileBox"); //Add button ID
var x = InputsWrapper.length; //initlal text box count
var FieldCount=1; //to keep track of text box added
$(AddButton).click(function (e) //on add input button click
{
if(x <= MaxInputs) //max input box allowed
{
FieldCount++; //text box added increment
//add input box
$(InputsWrapper).append('<div><input type="text" name="mytext[]" id="field_'+ FieldCount +'" value="Text '+ FieldCount +'"/><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="removeclass"><input type="button" value="刪除"></a></div>');
x++; //text box increment
}
return false;
});
$("body").on("click",".removeclass", function(e){ //user click on remove text
if( x > 1 ) {
$(this).parent('div').remove(); //remove text box
x--; //decrement textbox
}
return false;
})
});
</script>
</body>
</html>
運(yùn)行效果圖如下:

更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery常見事件用法與技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery操作json數(shù)據(jù)技巧匯總》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery常見經(jīng)典特效匯總》、《jQuery動(dòng)畫與特效用法總結(jié)》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計(jì)有所幫助。
- jQuery/JS監(jiān)聽input輸入框值變化實(shí)例
- jQuery實(shí)現(xiàn)input輸入框獲取焦點(diǎn)與失去焦點(diǎn)時(shí)提示的消失與顯示功能示例
- 基于Bootstrap使用jQuery實(shí)現(xiàn)輸入框組input-group的添加與刪除
- js與jquery實(shí)時(shí)監(jiān)聽輸入框值的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)文章
JavaScript判斷元素是否在可視區(qū)域的三種方法
這這篇文章給大家總結(jié)了JavaScript判斷元素是否在可視區(qū)域的三種方法,getBoundingClientRect,IntersectionObserver和offsetTop、scrollTop這三種方法,文中通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
JavaScript 數(shù)據(jù)元素集合與數(shù)組的區(qū)別說明
我們在獲取一組頁面元素時(shí)常會用到getElementsByName()或是getElementsByTagName()方法。2010-05-05
js實(shí)現(xiàn)局部頁面打印預(yù)覽原理及示例代碼
js 如何打印預(yù)覽,實(shí)局部打印頁面很簡單。就是把你需要打印的部分做一個(gè)起始標(biāo)記,下面有個(gè)示例大大家不妨參考下2014-07-07
JS?限時(shí)限次數(shù)點(diǎn)擊按鈕的實(shí)現(xiàn)思路
這篇文章主要介紹了JS?限時(shí)限次數(shù)點(diǎn)擊按鈕,實(shí)現(xiàn)方法很簡單需要用一個(gè)變量作為計(jì)數(shù),點(diǎn)擊一次,計(jì)數(shù)加一點(diǎn)擊函數(shù)內(nèi)判斷計(jì)數(shù)變量設(shè)置定時(shí)恢復(fù),對實(shí)例代碼感興趣的朋友一起看看吧2022-03-03
javascript仿百度輸入框提示自動(dòng)下拉補(bǔ)全
這篇文章主要介紹了javascript仿百度輸入框提示自動(dòng)下拉補(bǔ)全的相關(guān)資料,需要的朋友可以參考下2016-01-01

