jQuery重置表单2

方法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');" />

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”

Wha-ha! Just use

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

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

Thanks Konstantin, easy solution

this doesnt work..
someone please help…

add this at the end of the form

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

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()

DownsTream

Thanks…

easy solution but it work!!!

Bruno Soares

only this:

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

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

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

Thanks a lot for a good idea.

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

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

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

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

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.

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

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.

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

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();

@prabhakaran: Thanks! Great solution! :)

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

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();

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.

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/

nhat quan

alkahoma

Desert Fox

Try this

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

thanks you :)

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?

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值