方法1:$('#myform')[0].reset()

方法2:

How to .reset() form with jQuery

Such jQuery call won’t work:

1
2
    $('#formId').reset();// error: $("#formId").reset() is not a function

As the tutorial shows (yes, I never read manuals before I get into troubles as well, but always google answers before think, lol), we can have the following:

1
2
3
4
5
function resetForm(id) {$('#'+id).each(function(){this.reset();});
}

…and somewhere on a page this:

1
    <input type="button" onclick="resetForm('formId');" />

Tags: jQuery, tech notes, tricks

This entry was postedon Saturday, November 22nd, 2008 at 04:56 and is filed underjQuery.You can follow any responses to this entry through the RSS 2.0 feed.You can leave a response, or trackback from your own site.

30 Responses to “How to .reset() form with jQuery”

2009-07-26 @ 20:50
Konstantin

Wha-ha! Just use

$(‘#myFormId’)[0].reset();

2009-09-14 @ 08:48
Raymond

“$(’#myFormId’)[0].reset();”

Thanks Konstantin, easy solution

2009-11-17 @ 20:41
Lover of Sadness

this doesnt work..
someone please help…

2009-11-23 @ 21:27
prabhakaran

add this at the end of the form

and try this,
$(‘#rst_form’).click()

2009-11-23 @ 21:28
prabhakaran

add this at the end of the form
<div style=”display=’none’” ><input type=”reset” id=”rst_form”></div>

and try this,
$(‘#rst_form’).click()

2009-12-05 @ 12:04
DownsTream

Thanks…

easy solution but it work!!!

2010-01-12 @ 20:42
Bruno Soares

only this:

$(’form’)[0].reset();

get the form element…. and not the specific form directly: (‘#myform’)

2010-01-28 @ 23:09
deny

it work => $(‘#myform’)[0].reset()
Thanks for the idea

2010-03-08 @ 09:51
ดูหนัง

Thanks a lot for a good idea.

2010-07-17 @ 20:25
Axalix

well, it would better is everybody use all-around jQuery style, what means that correct solution is

$(’#myform’).get(0).reset();

2010-10-26 @ 17:06
CB

My shot:
$(resetSelector).bind(‘click’, function (event) {
event.preventDefault();
if(confirm(“Reset form?”)) {
$(this).closest(‘form’).get(0).reset();
}
});

2011-04-18 @ 01:43
Marek

just place somewhere in form button will be invisible and not clicable but:
anywhere in you scrip you can use: $(“my-reset-button”),click()
I’m tested it today – it works !

brgds, Marek

2011-07-02 @ 01:24
Dave

It wasn’t working for me until i moved the reset command from after a .prepend to before it. I guess don’t change any of the page content before attempting the form reset.

2011-10-05 @ 22:24
Your Marge

Is not workink for me, what does it mean $(”my-reset-button”)?

2011-10-15 @ 15:42
kevin

Good article but I agree with Konstantin, you don’t need to create a function just to do this. Also, you should look into custom jQuery functions rather than using vanilla javascript for this. Just a thought.

2012-05-24 @ 10:17
b

Doctors have treated a few of these babies with Viagra and observed vast improvements in their ability to get oxygen-rich blood from the lungs to other parts of the body.viagra http://www.tatansa.fr

2012-08-20 @ 09:52
Ejder Yurdakul

the function with dollar sign returns a jQuery object and not the dom element itself. The solution is mentioned above in other comments but i’d like to introduce a jQuery function “get” which returns the dom element itself;

$(“#”+form_id).get(0).reset();

2012-09-17 @ 19:24
Lillem4n

@prabhakaran: Thanks! Great solution!

2012-09-18 @ 18:00
How to .reset() form with jQuery | imluxin

[...] From: http://simple.procoding.net/2008/11/22/how-to-reset-form-with-jquery/ [...]

2012-09-20 @ 16:24
How to .reset() form with jQuery » imLuxin

[...] From: http://simple.procoding.net/2008/11/22/how-to-reset-form-with-jquery/ May 7, 2012 | Posted in: JS | Tags: jQuery | No Comments » [...]

2013-01-19 @ 17:23
Tai Nguyen

Sheesh, youngsters nowadays. It’s like they’ve completely forgotten that jQuery is a framework on top of Javascript. Does anyone even know Javascript anymore? Apparently not.

Let me explain exactly what happens when you run $(“#myform”)[0].reset();

1) jQuery will get the DOM element based on the ID you gave it.
2) It wraps that DOM element in a jQuery object with a bunch of other junk
3) It then returns a jQuery object with the DOM element in the zero index position.
4) When you request the zero-index array, you receive the DOM element back again.
5) You run the reset() method which is a native Javascript method that only works on DOM elements.

Oh and by the way, jQuery has to execute about 50 lines of code and do a bunch of extra crap just to return that jQuery object. And what does it use to get that original DOM element? Plain old Javascript: elem = document.getElementById( match[2] )

So can you see how insanely stupid it is to ask jQuery to run through all that garbage when all you had to do in the first place was:

document.getElementById(“#myform”).reset();

2013-01-23 @ 19:02
Doctor in NYC

Thank you for that enlightening writeup. If any reader needs to visit a proficient NYC Doctor, contact our clinic by calling us through our contact number 212-740-4600. We are affectionate in keeping our patients free from harm and healthy all throughout the year.

2013-01-28 @ 05:12
Robin

Wow. The .each() method is kinda overkill if you’re just targeting one element.
I’ve wrote a small blog post about the topic myself including examples of the most common pitfalls when trying to reset a jQuery form and the shortest code possible to reset a form. It can be found here:

http://forwebonly.com/how-to-reset-an-html-form-with-jquery/

2013-02-10 @ 16:03
nhat quan

alkahoma

2013-02-20 @ 07:33
Desert Fox

Try this

$(‘form’).trigger(‘reset’)

2013-02-27 @ 09:29
vhgnvgh

ftyj

2013-03-29 @ 08:47
dhivya

thanks you

2013-05-03 @ 03:38
prezzo viagra

Working out the root problem from which the root was taken.
Sarah Friar – Goldman Sachs. Approach it the same way as right wingers want to regulate a variety
of industries, we are looking for a system that relies on biomass for energy is greatly decreased.
En esos casos, el prezzo viagra relaja al joven y permite que deje sus temores a
un lado, explica el psic?

2013-05-06 @ 22:22
hackshacks

This is certainly attention-grabbing, You are a quite specialist blog writer. I’ve joined your current rss feed and look ahead of time to help in search of additional within your fantastic post. Furthermore, I’ve got shared your blog inside my social support systems

2013-05-20 @ 23:03
click here

Hi my family member! I want to say that this post is awesome,
great written and come with approximately all vital infos.
I’d like to look more posts like this .

Leave a Reply

Name:
required

Mail:
required (will not be published)

Website:

Comment:

转载于:https://www.cnblogs.com/lushijie/archive/2013/05/28/3311172.html

jQuery重置表单2相关推荐

  1. validate+jquery+ajax表单验证

    1.案例 1.1 Html form表单内容 <form class="cForm" id="cForm" method="post" ...

  2. html中表单的校验的插件,功能强大的jquery.validate表单验证插件

    本文实例为大家分享了jquery.validate表单验证的使用方法,供大家参考,具体内容如下 1 .表单验证的准备工作 在开启长篇大论之前,首先将表单验证的效果展示给大家. 1.点击表单项,显示帮助 ...

  3. jQuery的表单验证

    jQuery的表单验证 直接上代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...

  4. js 重置表单 reset form

    原文地址: http://www.hpboys.com/820.html 由于JQuery中,提交表单是像下面这样的: $('#yigeform').reset(); 但是,不幸的是,这样写的话,会有 ...

  5. 表单提交成功后重置表单

    js提供了reset()重置表单的方法,但jquery本身没有提供reset()方法, 这就导致了表单提交后jquery不能直接重置表单而使得用户可以重复提交,那怎么解决这个问题呢? 很简单,把jqu ...

  6. 实战课【1】jQuery实现表单校验及布局

    实战课[1]jQuery实现表单校验及布局 在学习完html,css,js,mysql,jdbc,Servlet,filter,ajax,maven等web基础知识后,开始进行项目实战.此文就会记录在 ...

  7. 整理的16个有用的jQuery Form(表单)验证教程

    表单在每个网站开发者必不可少的组成部份,而最烦繁的也是表单验证部份,借助于jQuery一些现有成熟的插件,可以大大减少我们的开发工作量以及减少很多重复出现的问题 ,这篇文章将整理出非常好的16篇非常有 ...

  8. 重置表单验证 清除表单校验信息

    重置表单验证 清除表单校验信息 提交表单验证的时间经常遇见的一个问题 再次打开时表单验证依旧存在,这种就很烦,用户体验有点差, 在使用Vant或Element UI框架时经常会遇到,再次打开表单时,原 ...

  9. 使用jQuery提交表单

    我想使用jQuery提交表单. 有人可以提供代码,演示或示例链接吗? #1楼 来自手册: jQuery Doc $("form:first").submit(); #2楼 您将必须 ...

最新文章

  1. 编译-C语言库FFTW支持iOS平台的静态库
  2. 新手做自媒体运营了解这几个步骤,运营效率提高5倍!(附教程)
  3. linux主流文件系统及日志和非日志文件系统的区别
  4. 传统公司部署OpenStack(t版)简易介绍(八)——Dashboard模块部署
  5. scala学习笔记-集合操作(15)
  6. 使用string.Format需要注意的一个性能问题
  7. 《MySQL 8.0.22执行器源码分析(4.1)Item_sum类以及聚合》
  8. 面试 技术 教训_我如何应用从失败的技术面试中学到的经验教训来获得5个工作机会
  9. Java内嵌Groovy脚本引擎进行业务规则剥离(一)
  10. 洛谷 P2486 [SDOI2011]染色 树链剖分
  11. 使用OData快速构建REST服务
  12. 你的脸儿圆又圆改变MSN头像(转)
  13. 印度程序员和中国程序员的区别
  14. 当前话题管理故事:厨师绑湖蟹 (转载)
  15. 编程中常用的英文单词
  16. 程序员不得不知道的英语词汇
  17. Windows网络活跃点决定使用的优先权
  18. adb wifi 连接设备
  19. 解决电脑开机-EFI PXE 0 for IPv4(XX-XX-XX-XX-XX-XX) boot failed.问题
  20. 微信小程序 - 一键复制功能

热门文章

  1. Oracle 中伪数列ROWID
  2. Normal Map Compress with DXT5
  3. 工厂支持多数据库开发的三层结构模式随笔(一)
  4. PetShop 4.0 SQLHelper
  5. MapReduce之OutputFormat理解
  6. ElasticSearch核心基础之索引管理
  7. spark streaming性能优化
  8. (76)时序分析基础(基本资源)
  9. (96)FPGA面试题-Verilog设计半加器
  10. (68)Verilog HDL系统函数和任务:$random