下面是一個將人們重定向到同名頁面的代碼。例如,如果我輸入單詞“chocolate”并單擊“提交”,用戶應該被重定向到名為“chocolate.html”的同名頁面等。
此代碼僅在 <form> 引數標簽被洗掉時有效,如果洗掉涉及手動單擊 [Submit] 按鈕以重定向到同名的 .html 頁面(而不是在按 [Enter] 或 [Return] 時重定向鍵盤上的鍵)。
當我添加 <form> 引數標記時,此代碼不起作用;它只有在洗掉時才有效。我已經處理了好幾個小時來讓它與 <form> 標簽一起作業。有任何想法嗎?
這是代碼:
注意:如果您洗掉表單標簽,它會起作用,但僅當您手動單擊“提交”按鈕時。
這就是我到目前為止所得到的,我現在只需要在人們按下鍵盤上的 Enter 鍵時自動單擊按鈕。:)
更新:首先,非常感謝您迄今為止的幫助。
更新:代碼現在成功重定向到同名的 .html 頁面,但用戶需要手動單擊 [提交] 按鈕來完成此操作。從這里開始,我只需要找到一種方法,只要用戶在鍵盤上按下 [Enter],就會自動選擇/單擊 [Enter] 按鈕。:)
<input id="test" type="text" autofocus>
<button type="button" onclick="redirect()">Submit</button>
<script>
function redirect()
{
var url=document.getElementById('test').value
window.location = url ".html"
}
</script>
uj5u.com熱心網友回復:
向按鈕添加型別屬性,如下所示:
<button type="button" onclick="redirect()">Submit</button>
默認情況下,如果未設定,則假定按鈕的型別為submit,因此會將您的表單提交給表單的 action 屬性中提供的操作。設定 type 屬性以button防止這種默認行為。
看到您的表單中沒有指定操作屬性,當前頁面被假定為要重定向到的操作,因此表單實質上會重新加載當前頁面。
uj5u.com熱心網友回復:
這是新事物。無論如何,當您將表單標簽保留在那里時,它沒有任何動作屬性使您保持在同一頁面中。當表單標簽存在時,您無法點擊重定向腳本。
當您洗掉表單標簽時,它會點擊 JS 并激活代碼。看到表單標簽,我假設您正在嘗試發送一些資料。
建議您使用 jquery 和 ajax 來操作這些資料。或者你可以討論更多。
uj5u.com熱心網友回復:
在你的函式之后添加這個:
var test_keydown = document.getElementById("test");
test_keydown.addEventListener("keydown", function (e) {
if (e.code === "Enter") { //checks whether the pressed key is "Enter"
redirect();
}
});
uj5u.com熱心網友回復:
我遇到了您的問題,基本上,您想redirect在用戶單擊提交按鈕或用戶按下ENTER鍵盤上的鍵時觸發功能。
你可以通過兩種方式做到這一點 -
- 如果您希望用戶在頁面上的任意位置按下回車鍵時重定向,那么您需要
keyup在物件上添加事件,請參見下面的代碼 -當用戶單擊鍵盤上的鍵時document它將觸發函式。redirectENTER
<input id="test" type="text" autofocus>
<button type="button" onclick="redirect()">Submit</button>
<script>
function redirect()
{
var url=document.getElementById('test').value
window.location = url ".html"
}
document.addEventListener("keyup", function(event) {
if (event.keyCode === 13) { //13 is enter keycode
event.preventDefault();
redirect();
}
});
</script>
- 另一種方法是在自身上添加
keyup事件input,然后呼叫該redirect函式。這是當您不想在整個檔案上觸發重定向時,但僅在用戶完成輸入input然后按時ENTER。
<input id="test" type="text" autofocus onkeyup="handleKeyUp(event)">
<button type="button" onclick="redirect()">Submit</button>
<script>
function redirect()
{
var url=document.getElementById('test').value
window.location = url ".html"
}
function handleKeyUp(event) {
if (event.keyCode === 13) { //13 is enter keycode
redirect();
}
}
</script>
uj5u.com熱心網友回復:
如果您想在form標簽中執行此操作,請處理 on submit 事件。這也將同時處理輸入鍵,因為這是表單中的默認行為。
//Add the evnent listener unobtrusivly
document.getElementById("redirector").addEventListener("submit", function(event) {
//Stops the form submitting (its' default action)
event.preventDefault();
//Get the location
var redirectTo = document.getElementById('test').value ".html"
//bit of debugging
console.log("Redirecting to: " redirectTo)
//redirect
window.location.href = redirectTo;
})
<form id="redirector">
<input id="test" type="text" autofocus>
<!-- Note this will now submit -->
<button>Submit</button>
</form>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/429442.html
標籤:javascript html 重定向 搜索 输入
上一篇:使用Angular和ASP.NETCore將API呼叫重定向到Azure上的根url
下一篇:登錄后重定向到特定的帖子/URL
