温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

html动态添加input

发布时间:2020-06-09 21:51:59 来源:网络 阅读:1116 作者:许琴 栏目:web开发

 html动态添加input

 

html动态添加input

 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> 
  5. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> 
  6. <title>html动态添加input</title> 
  7. <style type="text/css"> 
  8. body { font:12px/1.5 tahoma, arial, \5b8b\4f53, sans-serif; } 
  9. ul { margin:0; padding:0; list-style:none; } 
  10. a { margin-left:5px; color:#07F; text-decoration:none; } 
  11. a:hover { text-decoration:underline; } 
  12. input { border:1px solid #ccc; margin:2px; } 
  13. table { border-collapse:collapse; border-spacing:0; } 
  14. td { margin:0; padding:10px; border:1px solid #ccc; } 
  15. </style> 
  16. <script type="text/javascript" src="jquery.min.js"></script> 
  17. <script type="text/javascript"> 
  18. $(function(){ 
  19.     $("#demo1").easyinsert();//最简单的应用 
  20.     $("#demo2").easyinsert({ 
  21.         name: ["demo2", "demo2"],//可以同时添加两个(或更多),name值相同也必须分开设置,name数组的长度就是每组input的个数。type、value、maxlength、className四种属性,若同组组员的设置一样,可以只设置一次。 
  22.         value: ["默认值2-1", "默认值2-2"],//可以给同组input分别设置默认值 
  23.         maxlength: 15,//每组input的maxlength都一样,无需使用数组 
  24.         className: ["demo2_class1", "demo2_class2"],//不用我解释了吧 
  25.         toplimit: 5,//可以添加组数上限(默认是0,表示无上限),它是总管,so,name相当于小组组长 
  26.         initValue: [//初始化的时候,各input的value就是归它管,必须是数组 
  27.             ["初始值2-1", "初始值2-2"] 
  28.         ] 
  29.     }); 
  30.     $("#demo3").easyinsert({ 
  31.         name: "demo3", 
  32.         toplimit: 2, 
  33.         initValue: [ 
  34.             ["初始值3-1"],//必须是数组,就算每组只有一个input 
  35.             ["初始值3-2"], 
  36.             ["初始值3-3"]//小三儿,别想蒙混过关,总管只允许添加两组 
  37.         ] 
  38.     }); 
  39.     $("#demo4").easyinsert({ 
  40.         name: ["demo4", "demo4", "demo4", "demo4", "demo4", "demo4"], 
  41.         type: ["text", "radio", "password", "checkbox", "file", "button"], 
  42.         value: ["我是text", "我是radio", "我是password", "我是checkbox", "", "我是button"] 
  43.     }); 
  44.     $("#demo5").easyinsert({//type新增custom和select 
  45.         name: ["demo5", "demo5", "demo5", "demo5"], 
  46.         type: ["custom", "text", "custom", "select"], 
  47.         value: ["<strong style=\"color:#ff7b0e;\">科目:</strong>", "", "<strong style=\"color:#ff7b0e;\">类型:</strong>", { '理论': '1', '技能': '2', '上机': '3' }], 
  48.         initValue: [ 
  49.             ["<strong style=\"color:#ff7b0e;\">科目:</strong>", "初始值5-1", "<strong style=\"color:#ff7b0e;\">类型:</strong>", { '理论a': '1', '技能a': '2', '上机a': '3' }], 
  50.             ["<strong style=\"color:#ff7b0e;\">科目:</strong>", "初始值5-1", "<strong style=\"color:#ff7b0e;\">类型:</strong>", { '理论b': '1', '技能b': '2', '上机b': '3' }] 
  51.         ] 
  52.     }); 
  53. }); 
  54.  
  55. /** 
  56.  * EasyInsert 4.0 
  57.  * http://IlikejQuery.com/EasyInsert 
  58.  * 
  59.  * @Creator   wo_is神仙 <jsw0528@MrZhang.net> 
  60.  * @Depend    jQuery 1.4+ 
  61. **/ 
  62.  
  63. ;(function($){ 
  64.     $.fn.extend({ 
  65.         "easyinsert": function(o){ 
  66.             o = $.extend({ 
  67.                 //触发器 
  68.                 clicker: null,//根据class(或id)选择,默认.next()获取 
  69.                 //父标签 
  70.                 wrap: "li", 
  71.                 name: "i-text", 
  72.                 type: "text", 
  73.                 value: "", 
  74.                 maxlength: 20, 
  75.                 className: "i-text", 
  76.                 //新增上限值 
  77.                 toplimit: 0,//0表示不限制 
  78.                 //初始化值,二维数组 
  79.                 initValue: null//用于修改某资料时显示已有的数据 
  80.             }, o || {}); 
  81.             var oo = { 
  82.                 remove: "<a href=\"#nogo\" class=\"remove\">移除</a>", 
  83.                 error1: "参数配置错误,数组的长度不一致,请检查。", 
  84.                 error2: "参数配置错误,每组初始化值都必须是数组,请检查。" 
  85.             } 
  86.             //容器 
  87.             var $container = $(this); 
  88.             var allowed = true
  89.  
  90.             //把属性拼成数组(这步不知道是否可以优化?) 
  91.             var arrCfg = new Array(o.name, o.type, o.value, o.maxlength, o.className); 
  92.             //arr ==> [name, type, value, maxlength, className]  
  93.             var arr = new Array(); 
  94.             $.each(arrCfg, function(i, n){ 
  95.                 if ( $.isArray(n) ) { 
  96.                     arr[i] = n; 
  97.                 } else { 
  98.                     arr[i] = new Array(); 
  99.                     if ( i === 0 ) { 
  100.                         arr[0].push(n); 
  101.                     }else{ 
  102.                         //补全各属性数组(根据name数组长度) 
  103.                         $.each(arr[0], function() { 
  104.                             arr[i].push(n); 
  105.                         }); 
  106.                     } 
  107.                 } 
  108.                 //判断各属性数组的长度是否一致 
  109.                 if ( arr[i].length !== arr[0].length ) { 
  110.                     allowed = false
  111.                     $container.text(oo.error1); 
  112.                 } 
  113.             }); 
  114.  
  115.             if ( allowed ) { 
  116.                 //获取触发器 
  117.                 var $Clicker = !o.clicker ? $container.next() : $(o.clicker); 
  118.                 $Clicker.bind("click", function() { 
  119.                     //未添加前的组数 
  120.                     var len = $container.children(o.wrap).length; 
  121.                     //定义一个变量,判断是否已经达到上限 
  122.                     var isMax = o.toplimit === 0 ? false : (len < o.toplimit ? false : true); 
  123.                     if ( !isMax ) {//没有达到上限才允许添加 
  124.                         var $Item = $("<"+ o.wrap +">").appendTo( $container ); 
  125.                         $.each(arr[0], function(i) { 
  126.                             switch ( arr[1][i] ) { 
  127.                                 case "select"://下拉框 
  128.                                     var option = ""
  129.                                     $.each(arr[2][i], function(i, n) { 
  130.                                         option += "<option value='"+ n +"'>"+ i +"</option>"; 
  131.                                     }); 
  132.                                     $("<select>", { 
  133.                                         name: arr[0][i], 
  134.                                         className: arr[4][i] 
  135.                                     }).append( option ).appendTo( $Item ); 
  136.                                     break; 
  137.                                 case "custom"://自定义内容,支持html 
  138.                                     $Item.append( arr[2][i] ); 
  139.                                     break; 
  140.                                 default://默认是input 
  141.                                     $("<input>", {//jQuery1.4新增方法 
  142.                                         name: arr[0][i], 
  143.                                         type: arr[1][i], 
  144.                                         value: arr[2][i], 
  145.                                         maxlength: arr[3][i], 
  146.                                         className: arr[4][i] 
  147.                                     }).appendTo( $Item ); 
  148.                             } 
  149.                         }); 
  150.                         $Item = $container.children(o.wrap); 
  151.                         //新组数 
  152.                         len = $Item.length; 
  153.                         if ( len > 1 ) { 
  154.                             $Item.last().append(oo.remove); 
  155.                             if ( len === 2 ) {//超过一组时,为第一组添加“移除”按钮 
  156.                                 $Item.first().append(oo.remove); 
  157.                             } 
  158.                         } 
  159.                         $Item.find(".remove").click(function(){ 
  160.                             //移除本组 
  161.                             $(this).parent().remove(); 
  162.                             //统计剩下的组数 
  163.                             len = $container.children(o.wrap).length; 
  164.                             if ( len === 1 ) {//只剩一个的时候,把“移除”按钮干掉 
  165.                                 $container.find(".remove").remove(); 
  166.                             } 
  167.                             //取消“移除”按钮的默认动作 
  168.                             return false; 
  169.                         }); 
  170.                     } 
  171.                     //取消触发器的默认动作 
  172.                     return false; 
  173.                 }); 
  174.                 //初始化 
  175.                 if ( $.isArray(o.initValue) ) {//判断初始值是否是数组(必需的) 
  176.                     $.each(o.initValue, function(i, n) { 
  177.                         if ( !$.isArray(n) ) { 
  178.                             $container.empty().text(oo.error2); 
  179.                             return false; 
  180.                         }else{ 
  181.                             if ( n.length !== arr[0].length ) { 
  182.                                 $container.empty().text(oo.error1); 
  183.                                 return false; 
  184.                             } 
  185.                         } 
  186.                         var arrValue = new Array(); 
  187.                         //初始值替换默认值 
  188.                         $.each(n, function(j, m) { 
  189.                             arrValue[j] = arr[2][j] 
  190.                             arr[2][j] = m; 
  191.                         }); 
  192.                         $Clicker.click(); 
  193.                         //默认值替换初始值 
  194.                         $.each(arrValue, function(j, m) { 
  195.                             arr[2][j] = m; 
  196.                         }); 
  197.                         //上面这种[移形换位法]不知道效率怎么样,我想不出别的更好的方法 
  198.                     }); 
  199.                 }else{ 
  200.                     $Clicker.click(); 
  201.                 } 
  202.             } 
  203.         } 
  204.     }); 
  205. })(jQuery); 
  206. </script> 
  207. </head> 
  208.  
  209. <body> 
  210. <table width="90%" align="center"> 
  211.     <tr> 
  212.         <td width="10%" align="right"><strong>Demo1</strong></td> 
  213.         <td width="90%"> 
  214.             <ul id="demo1"></ul> 
  215.             <a href="#">+ 添加</a> 
  216.         </td> 
  217.     </tr> 
  218.     <tr> 
  219.         <td align="right"><strong>Demo2</strong></td> 
  220.         <td> 
  221.             <ul id="demo2"></ul> 
  222.             <a href="#">+ 添加(最多5项)</a> 
  223.         </td> 
  224.     </tr> 
  225.     <tr> 
  226.         <td align="right"><strong>Demo3</strong></td> 
  227.         <td> 
  228.             <ul id="demo3"></ul> 
  229.             <a href="#">+ 添加(最多2项)</a> 
  230.         </td> 
  231.     </tr> 
  232.     <tr> 
  233.         <td align="right"><strong>Demo4</strong></td> 
  234.         <td> 
  235.             <ul id="demo4"></ul> 
  236.             <a href="#">+ 添加</a> 
  237.         </td> 
  238.     </tr> 
  239.     <tr> 
  240.         <td align="right"><strong>Demo5</strong> <sup style="color:#F00;">NEW</sup></td> 
  241.         <td> 
  242.             <ul id="demo5"></ul> 
  243.             <a href="#">+ 添加</a> 
  244.         </td> 
  245.     </tr> 
  246. </table> 
  247. </body> 
  248. </html> 

 

下载地址:

http://down.51cto.com/data/648996

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI