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}