1
我正在使用以下方法更改對應於表單上的輸入/下拉列表的查詢URL參數。目前window.location.href
將導致頁面重新加載更改。我如何整合history.replaceState()
而不用重新加載來修改網址?如何在不刷新的情況下修改URL查詢字符串
正則表達式的功能
function replaceQueryString(url, param, value) {
var re = new RegExp("([?|&])" + param + "=.*?(&|$)", "i");
if (url.match(re))
return url.replace(re, '$1' + param + "=" + value + '$2');
else
return url + '&' + param + "=" + value;
}
在輸入變化
$("#input_field").on('change', function(e) {
window.location.href = replaceQueryString(window.location.href, 'input_value', $(this).val());
});
更新
現在我已經成功地將網址更新爲值domain.com/2
上的變化,但我怎麼跑它通過replaceQueryString
函數將其解析爲domain.com/?input_value=2
$("#input_field").on('change', function(e) {
window.history.replaceState("#input_field", "input_value", $(this).val());
});
什麼是預期的結果,第三個參數? – guest271314
@ guest271314它應該將網址更改爲'domain.com/?input_value = 3&next_filter_value = 2'等。 – scopeak
代碼調用'.replaceState()'在哪裏? – guest271314