`
reymont
  • 浏览: 526242 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

jquery 表单 清空

阅读更多

做了个复杂查询的页面,字段太多了,填了一次,想清空挺麻烦的

$('#myform')[0].reset();

虽然reset方法可以做到一部分,但是如果你有个元素是这样的

<input name="percent" value="50"/>

那么点击reset只会还原成50






于是乎,有了以下方法,网上浏览过来,

$(':input','#myform')
 .not(':button, :submit, :reset, :hidden')
 .val('')
 .removeAttr('checked')
 .removeAttr('selected');

It is using the :input

selector which will match all input, textarea, select and button elements. Since we are passing #myform
as the second argument, it will only find inputs inside this form
element. Then it filters out all buttons, submits, resets and hidden
inputs using not()

. Then it is using val()

to set the value of the remaining fields to an empty string, and then it uses removeAttr

to remove the checked
and selected
attribute of the fields in case you have any radio/checkbox/select inputs. Tada.


很强大,包括了所有的情况


分享到:
评论
7 楼 珊水田园 2015-07-17  
确实很好用!
6 楼 lijunwyf 2014-10-29  
能否给个详细的示例?
5 楼 Tank03 2014-05-29  
display:none可以清除么
4 楼 coderStyle 2013-12-03  
灰常牛x,复制过来就可以用,谢过楼主分享。
3 楼 breadfruit 2013-04-18  
太彪悍了。
2 楼 f0799 2012-10-19  
easyui form clear 做的很烂,拿这个过去试试嘿嘿
1 楼 xiaochangui 2012-08-27  
$(':input','#myform') 
.not(':button, :submit, :reset, :hidden') 
.val('') 
.removeAttr('checked') 
.removeAttr('selected'); 

果然很好,很强大,直接拿来用就可以哦

相关推荐

Global site tag (gtag.js) - Google Analytics