我們在開發項目的時候都需要使用jQuery進行開發,那么jQuery要如何實現動態添加、刪除按鈕及input輸入框的方法呢?今天就讓錯新技術頻道小編和大家分享吧!
<html><head><meta charset="utf-8"><title>動態創建按鈕</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 allowedvar InputsWrapper = $("#InputsWrapper"); //Input boxes wrapper IDvar AddButton = $("#AddMoreFileBox"); //Add button IDvar x = InputsWrapper.length; //initlal text box countvar 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實現動態添加、刪除按鈕及input輸入框的方法,該介紹完成之后,就能進入項目體現效果,享受編程的樂趣了。
新聞熱點
疑難解答
圖片精選