1
我有一個問題,我原來的URL看起來像這樣:如何用jQuery替換查詢字符串值?
test.com/?manufacturer=0&body-style=0&min-price=270%2C000&max-price=780%2C000
正如你所看到的,最小的價格和最高價值的查詢字符串是不正確的,由於傳遞到逗號網址。它應該在它們各自的整數值中,如min-price = 270000和max-price = 780000。
我需要使用jQuery轉換min-max和max-price的查詢字符串值。我現在不知道如何去做。但我有代碼從URL中獲取它們,然後將它們轉換爲正確的值。我只是不知道如何使用jQuery將它們實現回URL(作爲新URL)。這是我現有的代碼:
//Function to get value of parameter in query string
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
//Function to remove commas and convert to number
function convert_to_pure_number(x) {
//Remove commas
var x_withoutcommas=x.replace(/,/g,'');
//Convert to plain number
var y =parseInt(x_withoutcommas ,10);
return y;
}
var min_price_original=getParameterByName('min-price');
var max_price_original=getParameterByName('max-price');
var min_price_converted=convert_to_pure_number(min_price_original);
var max_price_converted=convert_to_pure_number(max_price_original);
任何建議,我將如何繼續上面的代碼與額外的代碼把他們帶回網址貼?謝謝你的幫助。
UPDATE 這是這個過程: 表格將被髮送到服務器 - >網址將包含逗號 - >我的新代碼將刪除逗號 - >在查詢字符串值,修正值將使用。
乾杯。
太好了,我會嘗試這一個.. –
它的工作表示感謝。但是,如何更改發佈的網址以反映價格? –
我的回答中沒有看到任何'jQuery'代碼...也許標題問題和標記格式錯誤... @CodexMeridian如果此答案足夠好(如出現),請編輯您的問題和標題以接受純js代碼也是如此。 – gmo