如何设置每页列出的项目数、删除/添加 URL 参数以及设置所选选项全部点击?
我正在开发一个搜索页面的下拉菜单,该下拉菜单将允许用户选择他们想要在每页查看的项目数。当用户单击某个选项时,参数 &itemSize=*option value*
需要将其自身放置在 URL 中。
这部分我已经记下来了。问题是,如果 URL 已经包含 &itemSize=60
并且用户选择转到 90 个项目,我需要替换当前参数。替换其值或将其全部删除,然后将其替换为新的 &itemSize=
参数。我现在得到的是参数加倍 - &itemSize=60&itemSize=90
此外,我无法切换selected
选项,因为我不确定如何存储所选选项的值。
注意 - 每次选择该选项时都会重新加载页面。它不是通过 AJAX 完成的,目前这不是一个选择。无法使用插件。搜索结果最多可达 3000 条结果。我有一些代码,我认为可以用于处理重复参数的情况,但我不知道如何完成新选定选项的逻辑。
function js_sort(forms, type) {
var currentURL = window.location.href;
if (type == 1) {
var url = window.location.toString();
if (url.indexOf("&rows=") > 0) {
var clean_url = url.substring(0, url.indexOf("&rows="));
window.history.replaceState({}, document.title, clean_url);
}
location.href = clean_url + forms.value;
} //else if () {
//dont mind this, this is part of a diff drop down
//}
}
<div class="dropdown">
<select name="pagesize" onchange="js_sort(this,1)">
<option value="30" selected>30 Items Per Page</option>
<option value="60" selected>60 Items Per Page</option>
<option value="90" selected>90 Items Per Page</option>
<option value="120" selected>120 Items Per Page</option>
</select>
</div>
最佳答案
供接下来的读者找到解决方案
注意:更新为更通用的形式
window.location.href= window.location.href.replace(/&*itemSize=\d*/,'')+"&itemSize="+forms.value;
关于javascript - 如何设置每页列出的项目数并设置单击时选定的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48247020/