所以我正在建立一個電子商務網站,我在產品頁面。我想要一個過濾器系統,其中過濾器值附加到現有 URL 而不是加載最新過濾器引數的新 URL。
例如,如果我有兩個元素:
<input type="checkbox" name="checkbox1" value="test1">
和
<input type="button" name="button1" value="a button">
每一個都來自不同的形式。
假設我提交了第一個表單。網址變為:
.../?checkbox1=test1
當我還提交第二個表單時(在提交第一個表單之后),網址現在變為:
.../?button1=一個按鈕
但我希望將按鈕的值附加到第一個 url 中,例如:
.../?checkbox1=test1&button1=一個按鈕
我該怎么做?
謝謝
uj5u.com熱心網友回復:
有一個表格或這樣做
document.querySelector("[name=button1]").addEventListener("click",function(e) {
const chk = document.querySelector("[name=checkbox1]")
const but = this.value
let url = "/?button1=a button";
if (chk.checked) url =`&${chk.name}=${chk.value}`;
//location = url
console.log(url)
})
<input type="checkbox" name="checkbox1" value="test1">
<input type="button" name="button1" value="a button">
uj5u.com熱心網友回復:
你為什么不這樣走?只有單一形式:
<html>
<body>
<form action="redirect.html">
<input type="checkbox" name="checkbox1" value="test1"> <br><br>
<input type="button" name="button1" value="a button"> <br><br>
<input type="submit" name="submit-btn" value="submit">
</form>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/380326.html
