我有一個查詢字符串的URL
(例如:http://www.example.com/?page=index&opr=sales-current&pageno=1)
添加從下拉菜單中的數據爲字符串
我需要從「銷售當前的」改變「OPR」的值改爲「銷售 - [下拉值]」使用JavaScript或jQuery的
感謝
我有一個查詢字符串的URL
(例如:http://www.example.com/?page=index&opr=sales-current&pageno=1)
添加從下拉菜單中的數據爲字符串
我需要從「銷售當前的」改變「OPR」的值改爲「銷售 - [下拉值]」使用JavaScript或jQuery的
感謝
這是另一種方法。該代碼是有點長,作爲搜索字符串進行解析,以創建參數的映射,然後再建立起來(這應該與任何參數工作,然後):
var searchTerms = document.location.search.substr(1).split('&');
var parameters = {};
for(var i = 0; i < searchTerms.length; i++) {
var parts = searchTerms[i].split('=', 2);
parameters[parts[0]] = parts[1];
}
parameters['opr'] = 'sales-' + document.getElementById('selectboxID').value;
searchTerms = [];
for(var key in parameters) {
if(parameters.hasOwnProperty(key)) {
searchTerms.push(key + '=' + parameters[key]);
}
}
document.location.search = searchTerms.join('&');
Working Demo (我使用jQuery僅用於演示,實際的代碼是上面顯示的一個)
Url = document.location.href.split("?");
dropdownval = document.getElementByID('dropdown/selectbox id tag').value;
document.location = Url[0]+'?page=index&opr=sales-'+dropdownval+'&page=1';
這是你可以用它來實現這一目標的代碼。
謝謝,但我需要更好的解決方案,因爲「頁面」和「pageno」的值可能會改變 – 2010-11-24 08:34:14
或者你可以做一個取代:
var val = 'sales-' + document.getElementById('selectBox').value;
document.location.href = document.location.href.replace(/([?&]opr=)([^&]+)/, '$1' + val);
用#selectBox
的值(val
)替換opr
參數。
但是,我相信@Felix Kling的solution是更通用的方式。只是發佈一個簡單的解決方案提示。
感謝更簡單的解決方案,是的你是正確的「@Felix Kling 「解決方案更通用 – 2010-11-24 08:55:34
謝謝,它的工作和很好的演示也是,再次感謝 – 2010-11-24 08:54:24