想問一下我想讓網(wǎng)址get能夠累加上去比如說現(xiàn)在的網(wǎng)址是
region=abc&status=1
當(dāng)我可能進(jìn)入另一個網(wǎng)址後我想讓他在後面加上 &xxx=1但是不會洗掉前面的GET參數(shù)
region=abc&status=1&xxx=1
無論前面有多少GET參數(shù)都會加上去,但重複的就不加這樣我就不需要再網(wǎng)址那邊重新定義有幾個GET參數(shù)了只要網(wǎng)址上沒有相同的參數(shù)就累加上去這個有什麼代碼可以做到?
補(bǔ)充但如果網(wǎng)址沒有任何參數(shù) 此時要加參數(shù)時它能自動判別,也就是說變成
xxx.php?xxx=xxx
而不是&除非前面已經(jīng)有?了則會是&
4 回答

慕哥6287543
TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個贊
你可以試試這個
$url = 'xxx.php?xxx=xxx';
$params = [
'region' => 'abc',
'status' => 1
];
$urlParams = http_build_query($params);
if(strpos($url, '?')){
echo $url.$urlParams;
}else{
echo $url.'?'.$urlParams;
}

三國紛爭
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個贊
你好這個問題很簡單:
1)一般這種需求用在商品列表條件篩選的業(yè)務(wù)上面
比如:鞋 我想選擇紅色,42號
2)使用js先獲取當(dāng)前的url,然后直接拼接到上面就可以了
代碼如下:
var host = window.location.href; //獲取url
window.location.href = url+ "/xxx/1"; //拼接url
解決問題

GCT1015
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超4個贊
// 先來一個也是在當(dāng)前網(wǎng)站上看到的函數(shù)
function parse_url(url)
{
var a = document.createElement('a');
a.href = url;
return {
source:url,
protocol:a.protocol.replace(':', ''),
host:a.hostname,
port:a.port,
query:a.search,
params:(function(){
var ret = {}, seg = a.search.replace(/^\?/, '').split('&'), len = seg.length, i = 0, s;
for(;i<len;i++) {
if(!seg[i]) {
continue;
}
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file:(a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
hash:a.hash.replace('#', ''),
path:a.pathname.replace(/^([^\/])/, '/$1'),
segments:a.pathname.replace(/^\//, '').split('/')
};
}
- 通過js將所有鏈接添加點(diǎn)擊事件
- 事件中通過parse_url解析當(dāng)前頁面地址及目標(biāo)頁面地址
- 合并解析后兩地址的請求參數(shù)
- 拼接最終跳轉(zhuǎn)地址
- 阻止默認(rèn)行為
如果要通過php的話, 那得通過函數(shù)來生成鏈接
- 解析目標(biāo)鏈接
- 合并參數(shù)
- 返回最終訪問鏈接
- 4 回答
- 0 關(guān)注
- 682 瀏覽
添加回答
舉報
0/150
提交
取消