jquery clears the form some methods summary

Source: Internet
Author: User

Commonly used emptying a form

The code is as follows Copy Code

$ (': input ', ' #myform ')
. Not (': button,: Submit,: RESET,: Hidden ')
. Val (")
. removeattr (' checked ')
. Removeattr (' selected ');


Cases

The code is as follows Copy Code

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

Although the Reset method can do a part, but if you have an element that is like this

The code is as follows Copy Code
<input name= "percent" value= "/>"

Then click Reset will only revert to 50

Sometimes you might want to do this by submitting the form with Ajax, emptying the form after the form has been successfully submitted, and following this code:

The code is as follows Copy Code

<form>
<input name= "Name1"/><br/>
<input name= "Name1"/><br/>
<textarea name= "Content" ></textarea>
<a href= "javascript:void (0)" onclick= "subform ()" > Submit </a>
</form>

After clicking Submit, the content is submitted via Ajax.
I used the following code to clear the form

The code is as follows Copy Code


$ ("input"). Val (');
$ ("textarea"). Val (');

The previous days have no intention of thinking of another method (if you know the following method friends can stop there):

  code is as follows copy code


//In form forms Add a hidden reset button,
<input type= "reset" style= "Display:none;"/>
//Then trigger the Reset button
function by trigger Subform () {
/* ...
* Submit the code portion of the form
* ... * *
$ ("Input[type=reset]"). Trigger ("click");//Trigger reset button}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.