2012-01-20 3 views
1

사이트가 처음으로로드되는 경우 사용자가 병원의 위치 드롭 다운을 선택하면 URL에 쿼리 문자열로 hos = somelocation을 전달해야합니다. http://mysite/events/Pages/default.aspx?hos=MunsterURL에 쿼리 문자열이 이미 있는지 jquery 확인

URL이 이미 나는 이미 존재하는 경우 & 호는 이미 window.location.search..and에 있는지를 확인하기 위해 필요 http://mysite/events/Pages/default.aspx?kwd=cancer&hos=Munster

같은 다른 쿼리 문자열과 함께 하나가있는 경우, 가지고있는 값을 다음과 같이 최근에 선택한 값으로 바꾸지 말고 바꾸십시오 : http://mysite/events/Pages/default.aspx?kwd=cancer&hos=Munster&hos=Carmel -> 이는 내가 원하는 것이 아닙니다. 나는 이것이 사용자가 Dropdown 위치에서 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

는 자바 스크립트를 사용할 수는 [좋은 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; 
} 
관련 문제