Resetting a form does not work

I use the following code to reset form fields.

document.getElementById("form1").reset();//form1 is the form id.

He does not work. I also tried using JQuery. Even jQuery also does not work.

$("#reset").click(function(){
$('form1')[0].reset();
});

My html code

<form name="form1" id="form1" method="post">
<h3>Personal Information</h3>
<h4>Name</h4>
<input type="text" id="fname" name="fname" maxlength=50 size=11/>
<input type="text" id="mname" name="mname" maxlength=15 size=8/>
<input type="text" id="lname" name="lname" maxlength=50 size=11/>
<input type="button" id="reset" value="Reset" onclick="Reset()"/>
</form>

I follow W3Schools . Here is my fiddle . Could you explain the mistake?

+4
source share
8 answers

The problem is that you are setting button identifiers "reset". Because of this, the reset form method was overwritten by the button element. Just use a different id for your button.

<form name="form1" id="form1" method="post">

<h3>Personal Information</h3> 

<h4>Name</h4>

    <input type="text" id="fname" name="fname" maxlength="50" size="11" />
    <input type="text" id="mname" name="mname" maxlength="15" size="8" />
    <input type="text" id="lname" name="lname" maxlength="50" size="11" />
    <input type="button" id="resetBtn" value="Reset" />
</form>

, . script.
fiddle.

+14

. "id=reset". reset. id="reset1".

+3

: Reset

<input type="reset" value="Reset"/>
+2

jQuery :

$('#form1') // Select with ID

$('form[name=form1]') // Select with name
+1

, $('form1'), form1, , id:

$('#form1')[0].reset();
+1

reset, :

<input type="reset" id="reset" value="Reset" onclick="this.form.reset();"/>
0

fiddle.

vanilla js :

...

document.getElementById("form1").reset();//form1 is the form id.

... reset. :

function Reset() {
    document.getElementById("form1").reset();//form1 is the form id.
}

jQuery :

, . . , "form1". , '# form1'. jQuery getElementByID. , getElementByID ; jQuery . , onClick jquery.

0

, jsfiddle: http://jsfiddle.net/ty9rU/17/ reset_btn

reset , .

0

All Articles