Javascript 如何重置输入字段?jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35492054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:48:26  来源:igfitidea点击:

How to reset input field? jQuery

javascriptjqueryhtml

提问by AlanH

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?

我已经阅读了几篇文章,包括thisthis,但我似乎无法在提交后清除输入字段。执行此操作的最基本/基本方法是什么?

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>

回答by stark

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 :inputselector):

为了以特定形式重置所有输入,您还可以使用以下代码(使用:input选择器):

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

回答by kodvin

This should work,(I've tested it myself).

这应该有效,(我自己测试过)。

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

回答by Omari Victor Omosa

This one worked for me; Few modification to. I use function(e)and e.preventDefault()and used $('input[name=checkListItem').val('');instead of reset()

这个对我有用;很少修改。我使用function(e)e.preventDefault()和使用$('input[name=checkListItem').val('');,而不是重置()

<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>

回答by Siddharth Shukla

We can write after submission.

提交后我们就可以写了。

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