javascript - Form's reset() method doesn't reset input with value attribute - Stack Overflow

Why second input with value attribute not being reset when I call the form's reset() method? And w

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
Add a ment  | 

4 Answers 4

Reset to default 2

You 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745357300a4624194.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信