简体   繁体   English

如何重置输入字段? jQuery的

[英]How to reset input field? jQuery

I've read several posts, including this , and this , but I can't seem to get the input field to clear out after submitting. 我已经阅读了几篇帖子,包括这个这个 ,但我似乎无法在提交后清除输入字段。 What is the most basic / elementary way to do this? 最基本/最基本的方法是什么?

Currently I have this: 目前我有这个:

$(document).ready(function(){
    $('form[name=checkListForm]').on('submit', function() {
          // prevent the form from submitting
          event.preventDefault();
          var toAdd = $(this).find('input[name=checkListItem]').val();
          $('.list').append("<div class='item'>" + toAdd + "</div>")
          $('input[name=checkListItem').reset();
        });
});

My HTML is: 我的HTML是:

<!DOCTYPE html>
<html >
  <head>
    <meta charset="UTF-8">  
    <link rel="stylesheet" href="stylesheet.css">     
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script type="text/javascript" src="script.js"></script>
  </head>
  <body>
    <h2>To Do</h2>
    <form name="checkListForm">
      <input type="text" name="checkListItem" />
      <button type="submit" id="button">Add!</button>
    </form>
    <br/>
    <div class="list"></div>
  </body>
</html>

Instead of resetting, just set a blank value to the input field using this bit of code : 不使用重置,只需使用以下代码将空白值设置为输入字段:

$('input[name=checkListItem').val('');

In order to reset all inputs in a particular form you could also use the following code (using the :input selector): 为了重置特定形式的所有输入,您还可以使用以下代码(使用:input selector):

$('form :input').val('');

This should work,(I've tested it myself). 这应该有用,(我自己测试过)。

$(document).ready(function(){
    $('form[name=checkListForm]').on('submit', function() {
          //other code
          $('input[name=checkListItem]').val("")
        });
});

This one worked for me; 这个对我有用; Few modification to. 很少修改。 I use function(e) and e.preventDefault() and used $('input[name=checkListItem').val(''); 我使用function(e)e.preventDefault()并使用$('input[name=checkListItem').val(''); instead of reset() 而不是重置()

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>

    $(document).ready(function(){
        $('form[name=checkListForm]').on('submit', function(e) {
              // prevent the form from submitting
              e.preventDefault();
              var toAdd = $(this).find('input[name=checkListItem]').val();
              $('.list').append("<div class='item'>" + toAdd + "</div>")


              $('input[name=checkListItem').val('');
            });
    });
 </script>

<h2>To Do</h2>
<form name="checkListForm">
  <input type="text" name="checkListItem" />
  <button type="submit" id="button">Add!</button>
</form>
<br/>
<div class="list"></div>

我们可以在提交后写。

document.getElementById("myform").reset();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM