在不重新加载的情况下,将表单的所有输入重置为默认值

Reset all inputs of a form to default values without reloading

本文关键字:表单 默认值 输入 情况下 新加载 加载      更新时间:2023-09-26

我想重置表单中输入的所有值(文本框、选择、复选框、单选按钮等),就像重新加载web一样,但当我触摸按钮时。

$("#button").click(function () {
     //Here the code that I need   
});

您可以使用JavaScript内置的reset()函数重置表单:

$("#otra").click(function () {
    $("#yourFormId")[0].reset();
});

工作演示:

$("#otra").click(function () {
  $("#yourFormId")[0].reset();
  return false; // prevent submitting
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="yourFormId">
  <input type="checkbox" name="myCB"/>
  <input type="text" name="myTB" value=""/>
  
  <button id="otra">Reset</button>
</form>

你可以这样做

$('input, textarea').val('');
$('select').find('option').prop("selected", false);

您可以使用此代码重置完整的表单

$('#myForm').trigger("reset");

试试这个:

$('form[name="myform"]')[0].reset();

我的页面在返回期间保留字段,reset()不会删除它们。

这帮助了我:https://stackoverflow.com/questions/6364289/clear-form-fields-with-jquery

$(".reset").click(function() {
    $(this).closest('form').find("input[type=text], textarea").val("");    
});

可以添加其他类型。