将用户重定向到选定的下拉列表



我希望用户在它选择的选项的基础上被重定向。这是我的HTML代码,代码已经在example.com/page,它重定向到example.com/page而不是example.com/page/1.请帮我整理一下

<select class="select-mob" id="select" onChange="fire()">
<option value="1"> Page 1</option>
<option value="2">Page 2 </option>
<option value="3">Page 3</option>
</select>

这里是JavaScript

function fire() {
var page = document.getElementByID('select').value;
if (page = "") {
window.location = "https://example.com/page"      
} else {
window.location= "https://example.com/page/" +page;     
}
}

您正在使用=而不是==/===
还将document.getElementByID更改为document.getElementById
给您:

function fire() {
var page = document.getElementById('select').value;
if (page == "") {
window.location = "https://example.com/page"      
} else {
window.location= "https://example.com/page/" +page;     
}
}

例子

您可以检查以下代码:

var page = document.getElementById('select').value;
if (page == "") {
window.location = "https://example.com/page"      
} else {
window.location= "https://example.com/page/" +page;     
}

试试这个

function fire() {
let page = document.querySelector('select').value;
page == "" ? window.location = "https://example.com/page" : window.location= "https://example.com/page/" +page; 
}

最新更新