jQuery: Clearing Form Inputs

前端 未结 5 1610
灰色年华
灰色年华 2020-12-22 23:27

I have tried to different ways to clear a form:

First Name:
5条回答
  •  青春惊慌失措
    2020-12-23 00:21

    Took some searching and reading to find a method that suited my situation, on form submit, run ajax to a remote php script, on success/failure inform user, on complete clear the form.

    I had some default values, all other methods involved .val('') thereby not resetting but clearing the form.

    I got this too work by adding a reset button to the form, which had an id of myform:

    $("#myform > input[type=reset]").trigger('click');
    

    This for me had the correct outcome on resetting the form, oh and dont forget the

    event.preventDefault();
    

    to stop the form submitting in browser, like I did :).

    Regards

    Jacko

提交回复
热议问题