Javascript 操作表单域的两个重要技巧

说明: 
技巧一:
当表单提交或重置时向客户端进行确认操作

技巧二: 
列出表单域中所有元素及其重要属性, 就是 input, select 等的 name, value 等.

总结:
用这两个技巧可以写一个通用的客户端表单验证函数, 至于怎么写, 动动脑筋就行了^^.
但是鄙人还是觉得, 不能过于依赖客户端的验证机制, 现在的人聪明得很, 只要花一点小心思, 就可以避过客户端的一切限制, 鄙人就乐于此道. 

目录:
1. 当表单提交或重置时向客户端进行确认操作
2. 列出表单域中所有元素及其重要属性


目录:
1. 当表单提交或重置时向客户端进行确认操作
    linenum
  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  2. <script type="text/javascript">
  3. //<![CDATA[
  4.     function fCfm(msg){
  5.         if(confirm(msg))return true;
  6.         return false;
  7.     } // shawl.qiu script
  8. //]]>
  9. </script>
  10. <form name="form1" id="form1" method="get" action=""
  11.     onsubmit="return fCfm('现在提交数据吗?');"
  12.      onreset="return fCfm('现在重置表单域所有内容吗?');" >
  13.     
  14.   <input name="textfield" type="text" value="tbx default value" />
  15.   <br />
  16.   <textarea name="textarea">txa default value</textarea>
  17.   <br />
  18.   <input type="submit" name="Submit" value="Submit" />
  19.   <input type="reset" name="Reset" value="Reset" />
  20. </form><br />
  21. <a href="?">back</a>

2. 列出表单域中所有元素及其重要属性
    linenum
  1. <script type="text/javascript"> 
  2. //<![CDATA[ 
  3.     function fListFmEle(obj){ 
  4.         try{ w.close(); } catch(e){} 
  5.          
  6.         w=open('', 'popup', 'width=500, height=500, left=200, top=100, scrollbars') 
  7.         w.document.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'); 
  8.   
  9.         for(var i=0; i<obj.length; i++){ 
  10.             w.document.write('obj name: ',obj[i].name.fontcolor('red'), 
  11.             '<br/>obj type: ',obj[i].type.fontcolor('red'), 
  12.             '<br/>obj.value: ', obj[i].value.fontcolor('blue'), 
  13.             '<p/>'); 
  14.         } 
  15.          
  16.         w.document.onclick=function(){ w.close(); } 
  17.         w.focus(); 
  18.     } // shawl.qiu script 
  19. //]]> 
  20. </script> 
  21. <a href="#" onclick="fListFmEle(document.fm);">list form elements</a><p/> 
  22. <form name="fm" id="fm" method="post" action=""> 
  23.       <input name="textfield" type="text" value="tbx value" /><br /> 
  24.     <input type="radio" name="RadioGroup1" value="radio" />Radio<br /> 
  25.     <input type="radio" name="RadioGroup1" value="radio" />Radio<br /> 
  26.     <input name="cbx" type="checkbox" id="cbx" value="checkbox" /> 
  27.     <input name="cbx1" type="checkbox" id="cbx1" value="checkbox" /> 
  28.     <input name="cbx2" type="checkbox" id="cbx2" value="checkbox" /><br /> 
  29.     <select name="select"> 
  30.       <option value="v">opt</option> 
  31.       <option value="v1">opt1</option> 
  32.     </select><br /> 
  33.     <select name="sle1" size="2" multiple id="sle1"> 
  34.       <option value="v">sle</option> 
  35.       <option value="v1">sle1</option> 
  36.       </select><br /> 
  37.     <textarea name="textarea">txa value</textarea><br /> 
  38.     <input type="submit" value="Submit" /> 
  39.     <input type="reset" value="Reset" /> 
  40. </form>