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');" /> |
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()
Thanks…
easy solution but it work!!!
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!![:)](http://simple.procoding.net/wp-includes/images/smilies/icon_smile.gif)
[...] From: http://simple.procoding.net/2008/11/22/how-to-reset-form-with-jquery/ [...]
[...] From: http://simple.procoding.net/2008/11/22/how-to-reset-form-with-jquery/ May 7, 2012 | Posted in: JS | Tags: jQuery | No Comments » [...]
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/
alkahoma
Try this
$(‘form’).trigger(‘reset’)
ftyj
thanks you![:)](http://simple.procoding.net/wp-includes/images/smilies/icon_smile.gif)
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 .