使用javascript将用户重定向到当前页面并使用一些查询字符串

问题描述 投票:23回答:10

当一个人点击链接时,我想这样做:

  1. 从文本框中抓取文本
  2. 编码文本
  3. 重定向到当前page.aspx?search =文本框值

到目前为止,我有这个,但它不起作用:

window.location.href = "?search=" + escape( $("#someId").val());
javascript jquery
10个回答
23
投票

我需要做一些类似于你想要的东西,所以我把一个允许你做的功能放在一起。我希望它有用。我已经对它进行了相当广泛的测试,没有发现任何问题,如果发现任何问题,请随时告诉我。

功能

function reloadWithQueryStringVars (queryStringVars) {
    var existingQueryVars = location.search ? location.search.substring(1).split("&") : [],
        currentUrl = location.search ? location.href.replace(location.search,"") : location.href,
        newQueryVars = {},
        newUrl = currentUrl + "?";
    if(existingQueryVars.length > 0) {
        for (var i = 0; i < existingQueryVars.length; i++) {
            var pair = existingQueryVars[i].split("=");
            newQueryVars[pair[0]] = pair[1];
        }
    }
    if(queryStringVars) {
        for (var queryStringVar in queryStringVars) {
            newQueryVars[queryStringVar] = queryStringVars[queryStringVar];
        }
    }
    if(newQueryVars) { 
        for (var newQueryVar in newQueryVars) {
            newUrl += newQueryVar + "=" + newQueryVars[newQueryVar] + "&";
        }
        newUrl = newUrl.substring(0, newUrl.length-1);
        window.location.href = newUrl;
    } else {
        window.location.href = location.href;
    }
}

如何使用它

该函数接受一个对象文字作为其唯一参数。对象文字应包含要重新加载页面的查询字符串变量。它会考虑现有变量,如果在传递的对象文字中指定现有变量,则会覆盖现有变量。

所以,让我们说我们的位置是http://localhost/helloworld.php,我想用一个查询字符串变量foo重定向到当前页面,其值应该是bar,我将调用该函数如下:

reloadWithQueryStringVars({"foo": "bar"});

浏览器将导航到http://localhost/helloworld.php?foo=bar。如果我通过以下功能:

reloadWithQueryStringVars({"foo": "bar2"});

浏览器将导航到http://localhost/helloworld.php?foo=bar2

由于函数接受对象文字,因此可以将多个属性传递给函数以获取多个查询字符串变量。如果我还在http://localhost/helloworld.php?foo=bar2,我将函数调用如下

reloadWithQueryStringVars({"foo": "bar3", "answer": "42", "bacon": "tasty"});

浏览器将导航到http://localhost/helloworld.php?foo=bar3&answer=42&bacon=tasty

你的用例

但是在回答这个问题时,你可以按如下方式调用该函数:

reloadWithQueryStringVars({"search": escape( $("#someId").val() )});

0
投票
    function Redirect()
      {
    window.location.href = "URL_TO_BE_Redircted";
      }

然后在按钮(asp.net)

    <asp:Button runat="server" id="btnRedirect" onClientClick="Redirect();return false;"/>

因此,在单击按钮后,控件将转到服务器端,然后在那里 您可以访问您形成的查询字符串!


8
投票

window.location.href = window.location.href + "?search=" + escape( $("#someId").val());


5
投票

怎么样:

window.location = "/currentpage.aspx?search=" + escape( $("#someId").val());

要么

window.location = window.location.pathname + "?search=" + escape( $("#someId").val());

等等...


2
投票

window.location.href添加内容的问题是,如果你已经这样做了?你只需要多次附加"?search=..."。更重要的是,它比它需要的更复杂。

你已经在使用jQuery了。为什么不这样做呢?

<form id="search" method="get">
<input type="text" name="search">
</form>
<a href="#" id="go">Search</a>

有:

$(function() {
  $("#go").click(function() {
    $("#search").submit();
    return false;
  });
});

然后你不必担心正确的URL,编码等。


1
投票

你需要预先添加实际的基本网址

window.location.href = window.location.href + "?search=" + escape( $("#someId").val());

1
投票

如果您只想更改搜索字符串,则会更改错误的位置属性。我想你想这样做:

location.search = "?search=" + encodeURIComponent( $("#someId").val());

1
投票
 $(document).ready(function(){
     $('a').click(function(e){

         window.location = "?search=" + encodeURIComponent($("#someId").val());

         //this line of code is intended for older ie and might work,
         //because I don't remember it exactly
        e.stopPropagation();

         return false;
     });                                                                   

});


0
投票

哪部分不起作用?

尝试encodeURI()而不是escape()。


0
投票
$("a").click(function() {

 document.location.href += "?search=" + encodeURIComponent($("#myTextBox").val());


});
© www.soinside.com 2019 - 2024. All rights reserved.