我有一個 html 表單,或多或少如下
<form action="/results">
<input name="q" type="text">
<div>See results</div>
</form>
如果我輸入一些東西到形式,如“我的搜索”,然后按“回車”我會被帶到結果頁面是這樣的:mywebsite.com/results?q=my search。我的問題是,當有人單擊“查看結果”時,我希望獲得相同的行為,這當前將他們帶到結果頁面但沒有引數。我知道使用<button>而不是<div>會得到我需要的結果,但在這種情況下,<button>由于所有模板的撰寫方式,使用是不切實際的。
uj5u.com熱心網友回復:
?q=searchstring分配給 時連接到 URL window.location。
document.querySelector("#results").addEventListener("click", function() {
let param = document.querySelector([name=q]).value;
let url = `/results?q=${encodeURIComponent(param)}`;
window.location = url;
}
<form action="/results">
<input name="q" type="text">
<div id="results">See results</div>
</form>
uj5u.com熱心網友回復:
如果您可以使用輸入型別提交,請嘗試將其包裝在 div 中,如下所示:
<form action="/results">
<input name="q" type="text">
<div><input type="submit" value="See results" /></div>
</form>
如果這對您不起作用,或者您可以使用上面的@Barmar 回答的 javascript 來處理它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/380324.html
標籤:javascript html 形式 模板 参数
