找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 221|回复: 0
打印 上一主题 下一主题

jquery无刷新添加和删除input输入框 增加减少input框

[复制链接]

3444

主题

3465

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11142
跳转到指定楼层
楼主
发表于 2016-8-17 08:51:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
XML/HTML Code
  • <a href="#" 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="#" class="removeclass">×</a></div>  
  • </div>  



JavaScript Code
  • <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="#" class="removeclass">×</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>  

    演示地址:http://www.freejs.net/demo/278/index.html




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

用户反馈
客户端