![](/img/trans.png)
[英]How to clear my input if button is clicked and at the same time make button unclickable if input is empty?
[英]How to clear input value and redirect at the same time
我想重定向到谷歌搜索頁面並同時清除輸入字段。
這是代碼(小提琴):
function eraseText() { document.getElementById("comp-kofkqaf0input").value = ""; }
<form action="https://www.google.com/search" class="searchform example" method="get" name="searchform" target="top"> <input name="sitesearch" type="hidden" value=""> <input type="text" placeholder="Search google" id="comp-kofkqaf0input" value="" style="padding-left: 18px;" name="q" value="" required> <button class="button1" type="submit" onclick="eraseText()"><span class="material-icons-outlined">search</span>Search</button> </form>
但是,HTML 只是刪除了我的輸入字段值,而不會將我重定向到 Google 搜索頁面。
當我單擊按鈕時,我需要將您重定向到 Google 搜索頁面並清除輸入字段中的值。
您應該在表單中添加 ID(以便於定位),然后在 eraseText() 中添加:
document.getElementById("formId").submit();
所以最終結果會更像這樣:
function eraseText() { document.getElementById("comp-kofkqaf0input").value = ""; document.getElementById("searchForm").submit(); }
<form action="https://www.google.com/search" class="searchform example" method="get" name="searchform" target="top" id="searchForm" > <input name="sitesearch" type="hidden" value="" > <input type="text" placeholder="Search google" id="comp-kofkqaf0input" value="" style="padding-left: 18px;" name="q" value="" required > <button class="button1" type="submit" onclick="eraseText()"><span class="material-icons-outlined">search</span>Search</button> </form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.