使用類似此代碼的代碼是否可以在提交時傳遞一個 html 錨點以跳轉到頁面中的特定位置?
onChange="document.form2.submit()"
我將上面的代碼用于選擇后提交的下拉選單,但我還需要它來傳遞頁面錨點..這可行嗎?
非常感謝
ps 錨點取決于使用的下拉選單。例如,一個頁面中有大約 40 個下拉選單。所以當你超過 20 個時,你必須繼續向下滾動以選擇下一個。這就是為什么我需要頁面在提交后跳轉到最后一個使用下拉...希望這是有道理的。
uj5u.com熱心網友回復:
url#your-hash在提交之前將表單的操作設定為正確。
function change_url(form, hash) {
form.setAttribute("action", "https://example.com/#" hash);
form.submit();
}
document.querySelectorAll("select").forEach(function(elem) {
elem.addEventListener("change", function() {
var hash = this.getAttribute("name");
var form = this.closest("form")
change_url(form, hash);
})
})
<h1>fill the form</h1>
<form action="" method="get" onsubmit="change_url(this); return false">
<a name="sel1"></a>
<select name="sel1">
<option></option>
<option>sel1</option>
</select>
<div style="height:500px"></div>
<a name="sel2"></a>
<select name="sel2">
<option></option>
<option>sel2</option>
</select>
<div style="height:500px"></div>
<a name="sel3"></a>
<select name="sel3">
<option></option>
<option>sel3</option>
</select>
<div style="height:500px"></div>
<button>submit</button>
</form>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/498239.html
標籤:javascript php html 改变 表格提交
上一篇:反應腳本沒有加載到網頁上?
下一篇:將for回圈縮短為一個
