form 表单元素重置(reset)解决方案

有初始值的 form 表单在点击默认的 Reset 按钮时,表单元素并不能清空,而是恢复初始值,相信这并不是我们期望的,本文介绍的是用 javascript 解决这一问题的方法。


--------------------------------------------------------------
点此浏览示例文件
--------------------------------------------------------------


Javascript:

   
   
  1.  
  2. <script language= "JavaScript" type= "text/javascript">
  3. <!--
  4. // 说明:有初始值的 form 表单元素重置(reset)解决方案
  5. // 整理:http://www.CodeBit.cn
  6.  
  7. function clearForm (formName ) {
  8. var formObj = document. forms [formName ];
  9. var formEl = formObj. elements;
  10. for ( var i= 0; i<formEl. length; i++ )
  11. {
  12. var element = formEl [i ];
  13. if (element. type == 'submit' ) { continue; }
  14. if (element. type == 'reset' ) { continue; }
  15. if (element. type == 'button' ) { continue; }
  16. if (element. type == 'hidden' ) { continue; }
  17.  
  18. if (element. type == 'text' ) { element. value = ''; }
  19. if (element. type == 'textarea' ) { element. value = ''; }
  20. if (element. type == 'checkbox' ) { element. checked = false; }
  21. if (element. type == 'radio' ) { element. checked = false; }
  22. if (element. type == 'select-multiple' ) { element. selectedIndex = -1; }
  23. if (element. type == 'select-one' ) { element. selectedIndex = -1; }
  24. }
  25. }
  26. //-->
  27. </script>
  28.  



示例:
HTML:

   
   
  1.  
  2. <form method="post" action="" name="testForm">
  3.  
  4. <input type="text" value="text" size="30" /> <br />
  5. <textarea name="" rows="3" cols="30">textarea </textarea> <br />
  6. a <input type="checkBox" name="a" value="a" />
  7. b <input type="checkBox" name="a" value="b" checked="checked" />
  8. c <input type="checkBox" name="a" value="c" checked="checked" />
  9. d <input type="checkBox" name="a" value="d" />
  10. e <input type="checkBox" name="a" value="e" /> <br />
  11. 2 <input type="radio" name="b" value="2" />
  12. 3 <input type="radio" name="b" value="3" checked="checked" /> <br />
  13.  
  14. test1: <select name="" multiple="multiple">
  15. <option value="11111111">11111111 </option>
  16. <option value="22222222" selected="selected">22222222 </option>
  17. <option value="33333333" selected="selected">33333333 </option>
  18. <option value="44444444">44444444 </option>
  19. <option value="55555555">55555555 </option>
  20. </select>
  21.  
  22. <br /> <br />
  23.  
  24. test2: <select name="">
  25. <option value="11">11 </option>
  26. <option selected="selected">22 </option>
  27. <option value="33">33 </option>
  28. <option value="44">44 </option>
  29. <option value="55">55 </option>
  30. </select>
  31.  
  32. <br /> <br />
  33.  
  34. <input type="submit" value="Submit" />
  35. <input type="reset" value="Reset" />
  36. <input type="button" value="Button" />
  37.  
  38. <input type="button" value="Javascript Clear" onclick="clearForm('testForm')" />
  39.  
  40. </form>
  41.  

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值