开发者

Adding data from dropdown to a string

开发者 https://www.devze.com 2023-01-26 11:47 出处:网络
I have a 开发者_C百科URL with query string (example: http://www.example.com/?page=index&opr=sales-current&pageno=1)

I have a 开发者_C百科URL with query string

(example: http://www.example.com/?page=index&opr=sales-current&pageno=1)

I need to change "opr" value from "sales-current" to "sales-[dropdown value]" using javascript or jQuery

Thanks


Here is another approach. The code is a bit longer, as the search string is parsed to create a mapping of the parameters and then build together again (this should work with any parameters then):

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

Reference: document.location

Working Demo (I use jQuery only for demonstration, the actual code is the one shown above)


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

That's the code you can use to achieve this.


Alternatively you could do a replace:

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

Replaces the opr parameter with the value (val) from #selectBox.

I believe, however, that @Felix Kling's solution is way more generic. Just posting to give a hint to a simple solution, too.

0

精彩评论

暂无评论...
验证码 换一张
取 消