2016-04-04 64 views
0

我的WordPress的網站有一個搜索表單,是廢話。什麼是通過JavaScript修改網址的最佳方法?

它生產這個搜索字符串:www.mysite.com/properties/?wpp_search[sort_order]=DESC&wpp_search[sort_by]=price&wpp_search[pagination]=on&wpp_search[per_page]=10&wpp_search[strict_search]=false&wpp_search[property_type]=residential_sales%2Cresidential_lettings%2Ccommercial%2Cfine_living&wpp_search[area]=&wpp_search[price][min]=&wpp_search[price][max]=&wpp_search[branch]=-1&wpp_search[bedrooms]=-1&wpp_search[department]=-1#propertycontent

什麼是最好的方式來抓住這個字符串,並改變某些方面,例如: [sort_order]=ASC [sort_by]=price

然後重新裝入顯示新的字符串,從而改變了頁面表格輸出?

+0

反序列化你的字符串,然後使用'location.href'應該可以工作。嘗試像'data = location.href.split('?')[1] .split('&')。map(a => a.split('='))' – somethinghere

+0

https://developer.mozilla .org/zh-CN/docs/Web/API/URL – Bergi

回答

0

您可以使用location.search獲取網址的查詢部分。查找參數,修改,重新構建您的url並將該值分配給location以使用該URL加載頁面。

1

試試這個。

url.replace('[sort_order]=DESC','[sort_order]=ASC').replace('[sort_by]=price','[sort_by]=newPrice'); 
+0

酷。這隻會替換命名變量嗎?那麼我只是強制重新加載頁面,還是我還得做別的事情? – GT22

+0

@ GT22它只是給你預期的URL。 – Lewis

相關問題