[英]Clear search after form submit
I would like the search bar to be cleared after the user presses the search button.我希望在用户按下搜索按钮后清除搜索栏。 Is there a quick workaround that is not invasive?
有没有一种非侵入性的快速解决方法?
<form method="POST" id="Submit">
<div class="inner-form">
<div class="input-field first-wrap">
<input id="search" name="input" placeholder="Paste here" type="text" required oninvalid="onInvalid(event)" />
</div>
<div class="input-field second-wrap">
<button id="button" class="btn-search" onclick="searchIt()" value="press" type="submit">
SEARCH
</button>
</div>
</div>
<p class="errorMessage" id="errorMessage"></p>
</form>
Bind the submit event to the <form>
:将提交事件绑定到
<form>
:
document.forms.Submit.onsubmit = e => /* ... */
then call the .reset()
method on the <form>
via e.target
, also wrap it in a setTimeout()
:然后通过
e.target
在<form>
上调用.reset()
方法,并将其包装在setTimeout()
中:
/* ... */ setTimeout(() => e.target.reset(), 0)
I have the <form>
sending data to a live test server and an <iframe>
to display the response so you can see that after a search the <input>
is cleared.我有
<form>
将数据发送到实时测试服务器和<iframe>
以显示响应,因此您可以看到搜索后<input>
被清除。
<form action='https://httpbin.org/post' method="POST" id="Submit" target='response'> <div class="inner-form"> <div class="input-field first-wrap"> <input id="search" name="input" placeholder="Paste here" type="text" required oninvalid="onInvalid(event)" /> </div> <div class="input-field second-wrap"> <button name='btn' class="btn-search" value="press" type="submit">SEARCH</button> </div> </div> <p class="errorMessage" id="errorMessage"></p> </form> <iframe name='response'></iframe> <script> document.forms.Submit.onsubmit = e => setTimeout(() => e.target.reset(), 0); </script>
You can delete the text on input search.您可以删除输入搜索中的文本。
function searchIt(){ document.getElementById('search').value=""; }
<form method="POST" id="Submit"> <div class="inner-form"> <div class="input-field first-wrap"> <input id="search" name="input" placeholder="Paste here" type="text" required oninvalid="onInvalid(event)" /> </div> <div class="input-field second-wrap"> <button id="button" class="btn-search" onclick="searchIt()" value="press" type="submit"> SEARCH </button> </div> </div> <p class="errorMessage" id="errorMessage"></p> </form>
function searchIt(e){ e.preventDefault(); /* do what you want submit form */ document.getElementById('Submit').reset() }
<form method="POST" id="Submit"> <div class="inner-form"> <div class="input-field first-wrap"> <input id="search" name="input" placeholder="Paste here" type="text" required oninvalid="onInvalid(event)" /> </div> <div class="input-field second-wrap"> <button id="button" class="btn-search" onclick="searchIt(event)" value="press" type="submit"> SEARCH </button> </div> </div> <p class="errorMessage" id="errorMessage"></p> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.