JQuery implements the form reset button reset to clear the form

Source: Internet
Author: User

Sometimes you may need to implement this effect: submit a form using ajax and clear the form after successfully submitting the form. The following code:
Copy codeThe Code is as follows:
<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, submit the content through ajax.
In the past, I used the following code to clear the form.
Copy codeThe Code is as follows:
$ ("Input"). val ('');
$ ("Textarea"). val ('');

I did not intend to think of another method a few days ago (if you know the following method, you can stop it ):
Copy codeThe Code is as follows:
// Add a hidden reset button to the form,
<Input type = "reset" style = "display: none;"/>
// Trigger the reset button
Function subform (){
/*...
* Code for submitting a form
*...*/
$ ("Input [type = reset]"). trigger ("click"); // trigger the reset button}

Related Article

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.