根据 URL 字符串中的变量填充属性值

发布于 2024-12-22 20:37:28 字数 783 浏览 2 评论 0原文

假设我在我的网站上的一个页面上: http://www.example.com /SearchResults.asp?Search=stackoverflow。是否可以使用 JavaScript 或 jQuery 代码从 URL 字符串中获取变量“search”并将其填充到搜索框的值中?

基本上,我将拥有常规识别的搜索框:

<input type="text" title="Search" id="search_box" />

用户将搜索某些内容,但我需要 JavaScript 或 jQuery 代码,当客户进行搜索时,该代码将从 URL 字符串中的“搜索”变量获取值.asp 页面并将其作为“值”添加到 input#search 中。

因此,用户将在此页面上: http://www.example.com/SearchResults。 asp?Search=stackoverflow 搜索框将如下所示:

<input type="text" title="Search" id="search_box" value="stackoverflow" />

谢谢

Let's say I'm on a page on my website: http://www.example.com/SearchResults.asp?Search=stackoverflow. Is it possible to use a JavaScript or jQuery code to get the variable "search" from the URL string and populate it into the value of the search box?

Basically I'll have my regular identified search box:

<input type="text" title="Search" id="search_box" />

And a user will search for something, but I will want a JavaScript or jQuery code that will get the value from the "search" variable in the URL string when the customer is on the Search.asp page and add it as the "value" to the input#search.

So the user will be on this page: http://www.example.com/SearchResults.asp?Search=stackoverflow and the search box will look like:

<input type="text" title="Search" id="search_box" value="stackoverflow" />

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自控 2024-12-29 20:37:28

您可以尝试这个函数:

function getSearchVariable(variable) 
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){
            return unescape(pair[1]);
        }else{
            return false;
        }
    }
}

如果您上面提到的示例中存在此函数,那么您只需调用 getSearchVariable("Search") 即可返回“stackoverflow”。

因此,在您的页面中,您将有一个如下所示的脚本元素:

<script type="text/javascript">
    function getSearchVariable(variable) 
    {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){
                return unescape(pair[1]).split("+").join(" ");
            }else{
                return "";
            }
        }
    }

    $(document).ready(function(){
        $("#search_box").val(getSearchVariable("Search"));
    });
</script>

希望有帮助!

You could try this function:

function getSearchVariable(variable) 
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){
            return unescape(pair[1]);
        }else{
            return false;
        }
    }
}

If this function is present in the sample you mentioned above, all you would call getSearchVariable("Search") to have "stackoverflow" returned.

So in your page, you would have a script element that looks like:

<script type="text/javascript">
    function getSearchVariable(variable) 
    {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){
                return unescape(pair[1]).split("+").join(" ");
            }else{
                return "";
            }
        }
    }

    $(document).ready(function(){
        $("#search_box").val(getSearchVariable("Search"));
    });
</script>

Hope that helps!

浅语花开 2024-12-29 20:37:28

您可以使用 JQuery URI 插件(真正由您无耻地编写)来提取 URL 的任何部分:具体来说, $.uri(window.location.href).at("query").search 会为所述 URL 生成“stackoverflow”。

总体流程是注册一个回调(在页面加载时触发)来分析 URL 并设置该表单元素的值,即:

<script>
  $(document).ready(function () {  
    $('#Search').val($.uri(window.location.href).at("query").search); 
  })
</script>

You can use the JQuery URI plugin (shamelessly written by yours truly) to extract any piece of the URL: Specifically, $.uri(window.location.href).at("query").search yields "stackoverflow" for the said URL.

The overall flow would be to register a callback (fired on page-load) to analyze the URL and set the value of that form element, that is:

<script>
  $(document).ready(function () {  
    $('#Search').val($.uri(window.location.href).at("query").search); 
  })
</script>
望她远 2024-12-29 20:37:28

JavaScript

document.getElementById('Search').value = [(window.location.search.match(/(?:^|[&;])Search=([^&;]+)/) || [])[1]];

jsFiddle

[] 包装它意味着如果找不到该值,它将返回一个空字符串而不是 undefined (它将被设置为输入的 并显示为 undefined 字符串)。

jQuery

$('#Search').val(function() {
    return (window.location.search.match(/(?:^|[&;])Search=([^&;]+)/) || [])[1];
});

jsFiddle

(/* get GET param */ || []) 的作用是,如果未找到 GET 参数,代码不会出错。

JavaScript

document.getElementById('Search').value = [(window.location.search.match(/(?:^|[&;])Search=([^&;]+)/) || [])[1]];

jsFiddle.

The wrapping it with [] means that if the value was not found, it will return an empty string instead of undefined (which would be set to the input's value and shown as the undefined string).

jQuery

$('#Search').val(function() {
    return (window.location.search.match(/(?:^|[&;])Search=([^&;]+)/) || [])[1];
});

jsFiddle.

The (/* get GET param */ || []) is so the code will not error if the GET param was not found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文