2012-01-20 72 views
1

如果是第一次加載站點,那麼當用戶選擇醫院的位置下拉菜單時,它應該將hos = somelocation作爲查詢字符串傳遞給URL。 http://mysite/events/Pages/default.aspx?hos=Munsterjquery檢查URL是否有查詢字符串

如果網址已經與其他查詢字符串像 http://mysite/events/Pages/default.aspx?kwd=cancer&hos=Munster

然後我需要檢查是否&居屋已經在window.location.search..and如果有一個已經沿一個,然後用最近選擇的值替換它所具有的值,而不是像這樣追加它: http://mysite/events/Pages/default.aspx?kwd=cancer&hos=Munster&hos=Carmel - >這不是我想要的。一旦用戶從位置下拉列表中選擇卡梅爾,我希望這是http://mysite/events/Pages/default.aspx?kwd=cancer&hos=Carmel。有人請幫助!

$(".LocationDropDown").change(function(e){ 
    var querystring=window.location.search; 
    var currentURL=$(location).attr('href'); 
    if(querystring=='') { 
     window.location.href= ($(this).val() == "All Hospitals") ? 'http://mysitee/events/Pages/default.aspx':'http://mysite/events/Pages/default.aspx?hos='+$(this).val(); 
     } 
     else    
      { 
      window.location.href = ($(this).val() == "All Hospitals") ? 'http://mysitee/events/Pages/default.aspx':currentURL+'&hos'+$(this).val(); 
    } 
}); 
+0

如果插件是一個選擇 - 這種解決方案可能就足夠了 - http://stackoverflow.com/questions/1090948/change-url-parameters-with-jquery – Anthony

+0

的JavaScript可以使用[一個很好的URI對象(http://stackoverflow.com/a/7853496/497418)... – zzzzBov

回答

0

嘗試BBQ plugin for jQuery。如果你打算使用多個查詢字符串值,比如你的例子,那麼使用起來非常簡單,並且是最好的插件。

爲了得到變量,你可以使用:

$.bbq.getState("kwd"); 
$.bbq.getState("hos"); 

,並設置狀態:

$.bbq.pushState({ hos: Carmel }); //pushState has an optional 2nd param 
//for whether or not to delete all the params not in the push function or to keep them. 

,並在URL上改變執行功能:

$(window).bind("hashchange", function(e) { 
    // In jQuery 1.4, use e.getState("url"); 
    var url = $.bbq.getState("url"); 
}); 
0

我討厭鏈接到網站,但我發現這個人的解決方案,它看起來相當不錯,通過JavaScript獲取和設置URL參數。

http://www.west-wind.com/weblog/posts/2009/Sep/07/Get-and-Set-Querystring-Values-in-JavaScript

//check if querystring contains hos 
if (getUrlEncodedKey('hos', location.search) != '') { 
    //set new value 
    setUrlEncodedKey('hos', newval); 
} 

getUrlEncodedKey = function(key, query) { 
    if (!query) 
     query = window.location.search;  
    var re = new RegExp("[?|&]" + key + "=(.*?)&"); 
    var matches = re.exec(query + "&"); 
    if (!matches || matches.length < 2) 
     return ""; 
    return decodeURIComponent(matches[1].replace("+", " ")); 
} 
setUrlEncodedKey = function(key, value, query) { 

    query = query || window.location.search; 
    var q = query + "&"; 
    var re = new RegExp("[?|&]" + key + "=.*?&"); 
    if (!re.test(q)) 
     q += key + "=" + encodeURI(value); 
    else 
     q = q.replace(re, "&" + key + "=" + encodeURIComponent(value) + "&"); 
    q = q.trimStart("&").trimEnd("&"); 
    return q[0]=="?" ? q : q = "?" + q; 
} 

//There are a couple of helpers in use here. For completeness here they are as well:  
String.prototype.trimEnd = function(c) { 
    if (c)   
     return this.replace(new RegExp(c.escapeRegExp() + "*$"), ''); 
    return this.replace(/\s+$/, ''); 
} 
String.prototype.trimStart = function(c) { 
    if (c) 
     return this.replace(new RegExp("^" + c.escapeRegExp() + "*"), ''); 
    return this.replace(/^\s+/, ''); 
} 

String.prototype.escapeRegExp = function() { 
    return this.replace(/[.*+?^${}()|[\]\/\\]/g, "\\$0"); 
}; 
0

的代碼只是一個提示,雖然沒有測試。

$(".LocationDropDown").change(function(e){ 
    var querystring=window.location.search; 
    var currentURL=$(location).attr('href'); 
    if(querystring=='') { 
     window.location.href= ($(this).val() == "All Hospitals") ? 'http://mysitee/events/Pages/default.aspx':'http://mysite/events/Pages/default.aspx?hos='+$(this).val(); 
    } 
    else if(str.indexOf("hos=")==-1) 
    { 
     window.location.href = ($(this).val() == "All Hospitals") ? 'http://mysitee/events/Pages/default.aspx':currentURL+'&hos='+$(this).val(); 
    } 
    else{ 
     window.location.href= ($(this).val() == "All Hospitals") ? 'http://mysitee/events/Pages/default.aspx': getReplacedValForHospital(currentURL, $(this).val()); 
    } 
}); 
function getReplacedValForHospital(currentURL, hospitalVal) { 
    var tempString = currentURL.substring(currentURL.indexOf("hos=")); 
    currentURL = currentURL.replace(tempString,"hos="+hospitalVal); 
    return currentURL; 
}