2010-11-24 53 views

回答

1

這是另一種方法。該代碼是有點長,作爲搜索字符串進行解析,以創建參數的映射,然後再建立起來(這應該與任何參數工作,然後):

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('&'); 

參考:document.location

Working Demo (我使用jQuery僅用於演示,實際的代碼是上面顯示的一個)

+0

謝謝,它的工作和很好的演示也是,再次感謝 – 2010-11-24 08:54:24

0
Url = document.location.href.split("?"); 
dropdownval = document.getElementByID('dropdown/selectbox id tag').value; 
document.location = Url[0]+'?page=index&opr=sales-'+dropdownval+'&page=1'; 

這是你可以用它來實現這一目標的代碼。

+0

謝謝,但我需要更好的解決方案,因爲「頁面」和「pageno」的值可能會改變 – 2010-11-24 08:34:14

0

或者你可以做一個取代:

var val = 'sales-' + document.getElementById('selectBox').value; 
document.location.href = document.location.href.replace(/([?&]opr=)([^&]+)/, '$1' + val); 

#selectBox的值(val)替換opr參數。

但是,我相信@Felix Kling的solution是更通用的方式。只是發佈一個簡單的解決方案提示。

+0

感謝更簡單的解決方案,是的你是正確的「@Felix Kling 「解決方案更通用 – 2010-11-24 08:55:34