2013-07-09 81 views
3

我現在有這個網址的地址欄window.location.href不工作,國防部重寫或jQuery的?

http://localhost/cp/subscribed&type=topics 

當我點擊search-button-cp應該加上&search=$term

$(".search-button-cp").live("click", function() { 
    var url = $(this).parents(".search-container:first").attr("data-url"); 
    var search = $(this).parents(".search-container:first").find(".search-input-cp").val(); 
    window.location.href = url + "&search=" + search; 
}); 

,而不是我得到這個網址

http://localhost/cp/localhost/cp/subscribed&type=topics&search=car 

alert(url),我得到localhost/cp/subscribed&type=topics ...所以有什麼問題?

應該重定向到該網址localhost/cp/subscribed&type=topics&search=car

如何解決這個問題?

這裏是我的國防部重寫櫃面它是罪魁禍首

RewriteRule ^cp/([A-Za-z0-9-]+)?(/[A-Za-z0-9-]+)?(/[A-Za-z0-9-]+)?([^.]+)?/?$ /cp.php?o=$1&id=$2&p=$3&query=$4 [L] 
+1

當你指定GET參數,你應該把一個**之前的第一個,不是** **? – Greg

回答

3

你應該把你的http://變種urlwindow.location.href或相對路徑的製作。

現在你的劇本「認爲」 localhost/cp/subscribed&type=topics是一個相對路徑(因爲沒有http(s)://)和你的基本路徑,http://localhost/cp/window.location.href路徑前加入。

+0

這可能足以使它與服務器相關,即預先加載「/」:'window.location.href =「/」+ url;' – Tommi

+0

啊,不,服務器名稱已經包含在url中。 – Tommi

0

試試這個函數:

function ChangeUrl() { 
    var currentUrl = window.location.href; 
    var stringToNavigate = ""; 
    if (currentUrl.indexOf("search") !== -1) 
     { 
      var stringToReplace = currentUrl.substring(currentUrl.indexOf("search"), currentUrl.length); 
      if (stringToReplace.indexOf("&") !== -1) 
      { 
       stringToReplace = stringToReplace.substring(0, stringToReplace.indexOf("&")); 
      } 
      stringToNavigate = currentUrl.replace(stringToReplace, "search=" + document.getElementById('yourTextBoxId').value);     
     } 
    else 
     { 
      stringToNavigate = currentUrl + "&search=" + document.getElementById('yourTextBoxId').value; 
     } 
    window.location.href = stringToNavigate; 
} 
+0

當我用javascript搜索時使用此代碼一次:) –