jquery
2012-08-09 57 views 1 likes 
1

我目前传递变量来查询字符串使用此选择下拉菜单和jQuery:使用jQuery追加多个变量来查询字符串

$("#filter_1").change(function(e) { 
    window.location.href = '?filter_1=' + $(this).val() 
}); 
$("#filter_2").change(function(e) { 
    window.location.href = '?filter_2=' + $(this).val() 
}); 

这是伟大的工作。但是,如果您有已经经过选择filter_2通过filter_1变量,filter_1被查询字符串覆盖。我现在需要的是追加filter_1filter_2和副-A-反之亦然的方式。有任何想法吗?

+1

更改queryparam重新加载页面。您是否也在页面加载时根据参数设置选择的值? – 2012-08-09 15:33:26

+0

为了增加Kevins响应。每次加载您的选择页面都会丢失选定的以前的值。您需要通过将选择传递给服务器并在页面加载时恢复它来处理它。 – anazimok 2012-08-09 15:55:14

回答

0

这里您创建解析查询字符串为你,如果它存在,或只是一个空字符串,如果它不返回所请求键的key=value功能。

$("#filter_1").change(function(e) { 
    window.location.href = '?filter_1=' + $(this).val() + 
     querystring('filter_2', '&'); 
}); 
$("#filter_2").change(function(e) { 
    window.location.href = '?filter_2=' + $(this).val() + 
     querystring('filter_1', '&'); 
}); 


function querystring(name, prefix) 
{ 
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 
    var regexS = "[\\?&]" + name + "=([^&#]*)"; 
    var regex = new RegExp(regexS); 
    var results = regex.exec(window.location.search); 
    if(results == null) 
    return ''; 
    else 
    return (prefix||'')+name+'='+ decodeURIComponent(results[1].replace(/\+/g, " ")); 
} 

功能来源于:How can I get query string values in JavaScript?

+0

这真的很接近!我不得不稍微编辑你的代码,但是我需要的是99%。谢谢!我不得不添加“&”分隔符来连接两个变量:'window.location.href ='?filter_1 ='+ $(this).val()+'&'+ querystring('filter_2') ;” – 2012-08-09 17:40:27

+0

@JacobWadenpfuhl更新。请注册。谢谢! – iambriansreed 2012-08-09 17:51:22

+0

我将完全给予好评,但它看起来像我没有足够的“信誉”呢。 – 2012-08-09 18:00:05

相关问题