福州網(wǎng)站建設>網(wǎng)站新聞>php技術

        jquery無刷新添加和刪除input輸入框 增加減少inpu

        發(fā)布日期:2018-02-23瀏覽次數(shù):1269 來源:福州網(wǎng)站建設

        XML/HTML Code
        1. <a href="#" id="AddMoreFileBox" class="btn btn-info">添加更多的input輸入框</a></span></p>  
        2. <div id="InputsWrapper">  
        3. <div><input type="text" name="mytext[]" id="field_1" value="Text 1"/><a href="#" class="removeclass">×</a></div>  
        4. </div>  

         

        JavaScript Code
        1. <script>  
        2. $(document).ready(function() {  
        3.   
        4. var MaxInputs       = 8; //maximum input boxes allowed  
        5. var InputsWrapper   = $("#InputsWrapper"); //Input boxes wrapper ID  
        6. var AddButton       = $("#AddMoreFileBox"); //Add button ID  
        7.   
        8. var x = InputsWrapper.length; //initlal text box count  
        9. var FieldCount=1; //to keep track of text box added  
        10.   
        11. $(AddButton).click(function (e)  //on add input button click  
        12. {  
        13.         if(x <= MaxInputs) //max input box allowed  
        14.         {  
        15.             FieldCount++; //text box added increment  
        16.             //add input box  
        17.             $(InputsWrapper).append('<div><input type="text" name="mytext[]" id="field_'+ FieldCount +'" value="Text '+ FieldCount +'"/><a href="#" class="removeclass">×</a></div>');  
        18.             x++; //text box increment  
        19.         }  
        20. return false;  
        21. });  
        22.   
        23. $("body").on("click",".removeclass"function(e){ //user click on remove text  
        24.         if( x > 1 ) {  
        25.                 $(this).parent('div').remove(); //remove text box  
        26.                 x--; //decrement textbox  
        27.         }  
        28. return false;  
        29. })   
        30.   
        31. });  
        32. </script>  

        以上是由福州網(wǎng)站建設的小編為你分享了"jquery無刷新添加和刪除input輸入框 增加減少inpu"文章,如果你在這方面有什么問題,隨時聯(lián)系我們

        php技術有關的文章
        如果您有什么問題,歡迎咨詢我們客服! 點擊QQ咨詢