jQuery實現(xiàn)動態(tài)添加、刪除按鈕及input輸入框的方法
更新時間:2017年04月27日 14:49:20 作者:David_黎
這篇文章主要介紹了jQuery實現(xiàn)動態(tài)添加、刪除按鈕及input輸入框的方法,涉及jQuery事件響應及頁面元素動態(tài)操作相關實現(xiàn)技巧,需要的朋友可以參考下
本文實例講述了jQuery實現(xiàn)動態(tài)添加、刪除按鈕及input輸入框的方法。分享給大家供大家參考,具體如下:
<html>
<head>
<meta charset="utf-8">
<title>動態(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>
運行效果圖如下:

更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jQuery常見事件用法與技巧總結》、《jQuery常用插件及用法總結》、《jQuery操作json數(shù)據(jù)技巧匯總》、《jQuery擴展技巧總結》、《jQuery拖拽特效與技巧總結》、《jQuery表格(table)操作技巧匯總》、《jQuery常見經典特效匯總》、《jQuery動畫與特效用法總結》及《jquery選擇器用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
您可能感興趣的文章:
- jQuery/JS監(jiān)聽input輸入框值變化實例
- jQuery實現(xiàn)input輸入框獲取焦點與失去焦點時提示的消失與顯示功能示例
- 基于Bootstrap使用jQuery實現(xiàn)輸入框組input-group的添加與刪除
- js與jquery實時監(jiān)聽輸入框值的oninput與onpropertychange方法
- jquery實現(xiàn)input輸入框實時輸入觸發(fā)事件代碼
- input 輸入框獲得/失去焦點時隱藏/顯示文字(jquery版)
- 基于jQuery的input輸入框下拉提示層(自動郵箱后綴名)
- input 和 textarea 輸入框最大文字限制的jquery插件
- jQuery 版本的文本輸入框檢查器Input Check
- jquery獲取input輸入框中的值
相關文章
JavaScript判斷元素是否在可視區(qū)域的三種方法
這這篇文章給大家總結了JavaScript判斷元素是否在可視區(qū)域的三種方法,getBoundingClientRect,IntersectionObserver和offsetTop、scrollTop這三種方法,文中通過代碼示例給大家介紹的非常詳細,需要的朋友可以參考下2023-12-12
JavaScript 數(shù)據(jù)元素集合與數(shù)組的區(qū)別說明
我們在獲取一組頁面元素時常會用到getElementsByName()或是getElementsByTagName()方法。2010-05-05

