admin管理员组文章数量:1422446
Why second input with value attribute not being reset when I call the form's reset()
method? And what is the best way to clean a form which has inputs with value attributes?
<form id="myform">
<input type="text" />
<input type="text" value="1" />
<button id="reset" name="reset" onclick="document.getElementById('myform').reset()">reset</button>
</form>
Why second input with value attribute not being reset when I call the form's reset()
method? And what is the best way to clean a form which has inputs with value attributes?
<form id="myform">
<input type="text" />
<input type="text" value="1" />
<button id="reset" name="reset" onclick="document.getElementById('myform').reset()">reset</button>
</form>
Share
Improve this question
edited Aug 19, 2019 at 14:21
f0ks
asked Aug 19, 2019 at 14:17
f0ksf0ks
471 silver badge4 bronze badges
2
-
DId you forget to put
myform
as ID in the above code? – Huangism Commented Aug 19, 2019 at 14:19 - @Huangism yes I did. But still not working. – f0ks Commented Aug 19, 2019 at 14:22
4 Answers
Reset to default 2You don't need JavaScript here. Just set the type
attribute of the button to reset
.
<form>
<input type="text" />
<input type="text" value="1" />
<button id="reset" name="reset" type="reset">reset</button>
</form>
By default, a button
element within a form
will submit the form.
Using JQuery
$("#reset").on("click" , function() {
$("input[type=text]").val("");
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<input type="text" />
<input type="text" value="1" />
<button id="reset" name="reset" type="button">reset</button>
</form>
Reset button reset form to initial values, not empty values. If you want to reset form pletely, you should use empty form and then set values manually after form is placed to document. For example, apply ids to field you want to preset and then use something like
const filedValues = [{
id: 'name',
value: '1'
},
{
id: 'text',
value: 'test text'
}
];
filedValues.forEach(fieldItem => {
const {
id,
value
} = fieldItem;
document.getElementById(id).value = value;
})
<form>
<input type="text" id="name" name="name" />
<input type="text" name="text" id="text" />
<button id="reset" name="reset" type="reset">reset</button>
</form>
Only one line code:
this.form.querySelectorAll('input[type=text]').forEach(function(input,i){input.value='';})
See:
<form>
<input type="text" value="0"/>
<input type="text" value="1" />
<button id="reset" name="reset" onclick="this.form.querySelectorAll('input[type=text]').forEach(function(input,i){input.value='';})">reset</button>
</form>
Note: no need to add type=reset
to button
本文标签: javascriptForm39s reset() method doesn39t reset input with value attributeStack Overflow
版权声明:本文标题:javascript - Form's reset() method doesn't reset input with value attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745357299a2655129.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论