3

我目前在地址栏中有这个网址

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......所以有什么问题?

它应该重定向到这个 urllocalhost/cp/subscribed&type=topics&search=car

我该如何解决这个问题?

这是我的mod重写,因为它是罪魁祸首

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]
4

2 回答 2

3

您应该http://在您的 var 之前url放置window.location.href或使路径相对。

现在您的脚本“认为”localhost/cp/subscribed&type=topics是一个相对路径(因为没有http(s)://),并且您的基本路径http://localhost/cp/被添加到window.location.href.

于 2013-07-09T13:14:29.320 回答
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;
}
于 2013-07-09T13:18:16.570 回答