有时候可能需要实现这样的效果:注册表单或者地址表单等填写多个记录之后,想要清除重新填写,如果一个个删除非常麻烦,因此这时清除按钮非常必须。接下来为您详细介绍两个自己经历的便捷方法,需要了解的朋友参考下:
方法一:
|
function clearForm(){ $(\':input\', form).each(function(){ var type=this.type; var tagName=this.tagName.toLowerCase(); if(type==\'text\'||type==\'password\'||type==\'textarea\'){ $(this).val(""); }else if(type==\'radio\'||type==\'checkbox\'){ this.checked = false; 或者:$(this).prop(false); }else if(tag==\'select\'){ this.selectedIndex = -1; } }); } |
方法二:
|
//在form表单中添加一个隐藏的reset按钮, <input type="reset" style="display:none;" /> //然后通过trigger来触发reset按钮 function subform(){ /* ... *提交表单的代码部分 * ... */ $("input[type=reset]").trigger("click");//触发reset按钮 } |