在 DOM 中,表单重置非常简单,使用 <input type=”reset”> 或者 <button type=”reset”> 即可。原生 DOM 也提供了 reset() 方法用于表单重置。
但是在 Firefox/Chrome 浏览器中,如果字段是隐藏域(type=”hidden”)的话,使用 reset 是无法将字段值还原为初始值的。但在 IE6/7/8 中都可以重置。
目前得到的解决方案是
<input type="text" name="aa" style="display:none;">
下面我给出一个完整的测试例子,我在ie和标准浏览器等数十种浏览器下面没有发现任何问题,有需要的同学们可以参考
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script> function inputValue(){ document.getElementById("aa").value="123456"; } function showValue(){ alert(document.getElementById("aa").value); } </script> <style> #aa{ display:none; } </style> </head> <body> <form id="form1" name="form1" method="post" action=""> <input type="text" name="textfield" id="textfield" /> <input type="text" name="textfield2" id="textfield2" /> <input type="text" name="textfield2" value="nnn" id="aa" /> <input type="button" onclick="inputValue()" value="value" /> <input type="submit" name="button" id="button" value="提交" /> <input type="reset" name="button2" id="button2" value="重置" /> </form> <a href="javascript:showValue()">show value</a> </body> </html>