2013-07-09 72 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搜索时使用此代码一次:) –