搜索查询参数Javascript



我想为具有相同键和不同值的查询字符串添加和设置参数。

url.com/product.php?order=price.low&order=quantity.low

所以有一个相同的键叫做order,它的值不同。我创建了一些更改产品排序的单选按钮。并添加这样的事件侦听器:

function lowQRadio() {
document.getElementById("radio-qlow").checked=true;
params.set("order","quantity.low");
window.location.href=(location.pathname + '?' + params);
}
function highQRadio() {
document.getElementById("radio-qhigh").checked=true;
params.set("order","quantity.high");
window.location.href=(location.pathname + '?' + params);

}
function  lowPRadio(){
document.getElementById("radio-plow").checked=true;
params.set("order","price.low");
window.location.href=(location.pathname + '?' + params);
}
function  highPRadio(){
document.getElementById("radio-phigh").checked=true;
params.set("order","price.high");
window.location.href=(location.pathname + '?' + params);
}

这个代码上面定义的一些变量,这里是变量:

let url = new URL(window.location.href);
let params = new URLSearchParams(url.search);
if(params.has("order")){
var aflag=params.getAll("order");
console.log(aflag.length);
if(aflag[aflag.length-1]==="quantity.low" || aflag[aflag.length-2]==="quantity.low") {
document.getElementById("radio-qlow").checked = true;
}
else if(aflag[aflag.length-1]==="quantity.high" || aflag[aflag.length-2]==="quantity.high"){
document.getElementById("radio-qhigh").checked=true;
}
else if(aflag[aflag.length-1]==="price.low" || aflag[aflag.length-2]==="price.low"){
document.getElementById("radio-plow").checked=true;
}
else if(aflag[aflag.length-1]==="price.high" || aflag[aflag.length-2]==="price.high"){
document.getElementById("radio-phigh").checked=true;
}

所以我想当用户选择按价格和数量排序时,两个单选按钮都会被检查和查询更改,如果用户选择一种排序方法,只有一个参数集,如果选择另一个,这两个都会被选中,页面将被刷新

php代码或后端工作,但js代码仅适用于一种类型的单选按钮,并且两种排序都不工作!!

请帮我解决这个问题,谢谢

查询字符串是一个名称-值对集合。名称必须唯一。你想做这样的事情吗url.html?OrderBy=col1、col2、col3

使用一个分隔字符串作为查询字符串参数值,然后在php代码中拆分该值,并使用逻辑按.应用顺序

最新更新