我有工作代码,可以重置我的表单时,我点击重置按钮。然而,当我的代码变长后,我意识到它不再工作了。

<div id="labels">
  <table class="config">
    <thead>
      <tr>
        <th colspan="4"; style= "padding-bottom: 20px; color:#6666FF; text-align:left; font-size: 1.5em">Control Buttons Configuration</th>
      </tr>
      <tr>
        <th>Index</th>
        <th>Switch</th>
        <th>Response Number</th>
        <th>Description</th>
      </tr>
    </thead>
    <tbody>            
      <form id="configform" name= "input" action="#" method="get">
        <tr>
          <td style="text-align: center">1</td>
          <td><img src= "static/switch.png" height="100px" width="108px"></td>
          <td id="small"><input style="background: white; color: black;" type="text" value="" id="number_one"></td>
          <td><input style="background: white; color: black;" type="text"  id="label_one"></td>
        </tr>
        <tr>
          <td style="text-align: center">2</td>
          <td><img src= "static/switch.png" height="100px" width="108px"></td>
          <td id="small"><input style="background: white; color: black;" type="text" id = "number_two" value=""></td>
          <td><input style="background: white; color: black;" type="text"  id = "label_two"></td>
        </tr>
        <tr>
          <td style="text-align: center">3</td>
          <td><img src= "static/switch.png" height="100px" width="108px"></td>
          <td id="small"><input style="background: white; color: black;" type="text" id="number_three" value=""></td>
          <td><input style="background: white; color: black;" type="text" id="label_three"></td>
        </tr>
        <tr>
          <td style="text-align: center">4</td>
          <td><img src= "static/switch.png" height="100px" width="108px"></td>
          <td id="small"><input style="background: white; color: black;" type="text" id="number_four" value=""></td>
          <td><input style="background: white; color: black;" type="text" id="label_three"></td>
        </tr>
        <tr>
          <td></td>
          <td><input type="submit" id="configsubmit" value="Submit"></td>
        </tr>
        <tr>
          <td><input type="reset" id="configreset" value="Reset"></td>
        </tr>
                  
      </form>
    </tbody>
  </table>
            
</div>

和我的jQuery:

$('#configreset').click(function(){
    $('#configform')[0].reset();
});

是否有一些源代码,我应该包括在我的代码中,以使.reset()方法工作?之前我用的是:

<script src="static/jquery.min.js"></script>
<script src="static/jquery.mobile-1.2.0.min.js"></script>

并且.reset()方法正在工作。

目前我正在使用

<script src="static/jquery-1.9.1.min.js"></script>      
<script src="static/jquery-migrate-1.1.1.min.js"></script>
<script src="static/jquery.mobile-1.3.1.min.js"></script>

这可能是原因之一吗?


当前回答

第一行将重置表单输入

$('form#myform').trigger("reset"); //Line1
$('form#myform select').trigger("change"); //Line2

第二个将重置select2

可选:如果使用不同的事件注册了不同的类型,则可以使用此选项

$('form#myform select, form input[type=checkbox]').trigger("change"); //Line2

其他回答

你可以像这样用id = resetform添加输入type = reset

<html>
<form>
<input type = 'reset' id = 'resetform' value = 'reset'/>
<!--Other items in the form can be placed here-->
</form>
</html>

然后在jquery中,你只需在id = resetform的元素上使用.click()函数,如下所示

<script> 
$('#resetform').click();
</script>

然后表单重置 注意:你也可以在css中使用id = resetform隐藏重置按钮

<style>
#resetform
{
display:none;
}
</style>

这是用普通Javascript比用jQuery更容易完成的事情之一。jQuery没有重置方法,但是HTML表单元素有,所以你可以像这样重置表单中的所有字段:

document.getElementById('configform').reset();

如果你这样做通过jQuery(见其他答案在这里:$('#configform')[0].reset()),[0]正在获取相同的表单DOM元素,你将直接通过document.getElementById。后一种方法更有效,也更简单(因为使用jQuery方法,您首先获得一个集合,然后必须从中获取一个元素,而使用普通Javascript,您只需直接获取元素)。

通过jquery函数. nearest (element)和.find(…)

获取父元素并寻找子元素。

最后,做所需的函数。

$("#form").closest('form').find("input[type=text], textarea").val("");

你可以尝试使用trigger() Reference Link

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

我使用这个简单的代码:

//reset form 
$("#mybutton").click(function(){
    $("#myform").find('input:text, input:password, input:file, select, textarea').val('');
    $("#myform").find('input:radio, input:checkbox').removeAttr('checked').removeAttr('selected');
});