Clear form fields with jQuery

前端 未结 30 3122
甜味超标
甜味超标 2020-11-22 15:48

I want to clear all input and textarea fields in a form. It works like the following when using an input button with the reset class:

$(\".reset         


        
30条回答
  •  长发绾君心
    2020-11-22 16:38

    if you use selectors and make values to empty values, it is not resetting the form, it's making all fields empty. Reset is to make form as it was before any edit actions from user after the load of form from server side. If there is an input with name "username" and that username was prefilled from server side, most of solutions on this page will delete that value from input, not reset it to the value how it was before user's changes. If you need to reset the form, use this:

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

    if you need not to reset the form, but fill all inputs with some value, for example empty value, then you can use most of the solutions from other comments.

提交回复
热议问题