Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Jul 20th, 2010 | Syntax: JavaScript | Size: 3.08 KB | Hits: 225 | Expires: Never
Copy text to clipboard
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3.     <head>
  4.         <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
  5.         <title>add input demo</title>
  6.     </head>
  7.     <body>
  8.         <form name='augmentform' action='#' method='post'>
  9.             <div id='input_wrap'>
  10.                 <p>
  11.                     <label>
  12.                         提交信息:
  13.                     </label>
  14.                     <input name='augmentinput'>
  15.                 </p>
  16.             </div>
  17.             <p>
  18.                 <input type='submit' value='提交'><input id='addinput' value='增加一个提交文本框' type='button'><input id='removeinput' value='删除一个' type='button'>
  19.             </p>
  20.             <p id='inputmessage'>
  21.             </p>
  22.         </form>
  23.         <script type='text/javascript'>
  24.             (function(w, undefined){
  25.                 var tool = function(){
  26.                     return {
  27.                         $: function(id){
  28.                             return document.getElementById(id);
  29.                         },
  30.                         bind: function(obj, type, fn){
  31.                             if (obj.attachEvent) {
  32.                                 obj['e' + type + fn] = fn;
  33.                                 obj[type + fn] = function(){
  34.                                     obj['e' + type + fn](w.event);
  35.                                 }
  36.                                 obj.attachEvent('on' + type, obj[type + fn]);
  37.                             }
  38.                             else {
  39.                                 obj.addEventListener(type, fn, false);
  40.                             }
  41.                         }
  42.                     };
  43.                 }(), addput = tool.$('addinput'), inputwrap = tool.$('input_wrap'), message = tool.$('inputmessage'), removeput = tool.$('removeinput'), maxsize = 5, z = function(){
  44.                     return inputwrap.getElementsByTagName('p')
  45.                 }();
  46.                 handleinput = {
  47.                     add: function(){
  48.                         if (z.length >= maxsize) {
  49.                             message.innerHTML = '最多增加' + maxsize + '条新信息';
  50.                             return false;
  51.                         }
  52.                         message.innerHTML = '';
  53.                         var newinput = '<p><label>提交内容:</label><input name="augmentinput"></p>';
  54.                         inputwrap.innerHTML += newinput;
  55.                     },
  56.                     remove: function(){
  57.                         if (z.length == 1) {
  58.                             message.innerHTML = '不能再删除了';
  59.                             return false;
  60.                         }
  61.                         message.innerHTML = '';
  62.                         inputwrap.removeChild(z[z.length - 1]);
  63.                     }
  64.                    
  65.                 };
  66.                
  67.                 tool.bind(addput, 'click', handleinput.add);
  68.                
  69.                 tool.bind(removeput, 'click', handleinput.remove);
  70.                
  71.             })(window);
  72.         </script>
  73.     </body>
  74. </html>