更改下拉列表时替换 URL 查询字符串值

发布于 2024-12-28 06:20:35 字数 1851 浏览 2 评论 0原文

我有一个具有双重角色的下拉菜单。用户可以访问该页面(http://mysite/events/Pages/default.aspx)直接使用下拉菜单,或者他们可以先进行搜索,然后通过选择下拉菜单进一步过滤搜索结果。 第一种情况的 URL 类似于 http://mysite/events/Pages/default.aspx?hos=Carmel和 第二种情况网址 http://mysite/events/Pages/default.aspx ?kwd=health&type=Events&hos=卡梅尔 这就是我现在正在做的事情,但它的行为很奇怪,并且对 url http://mysite.events/Pages/default.aspx?hos=Crown&hos=Carmel

因此,如果用户第一次从下拉列表中选择 carmel 并决定他/她想要查找 Indianapolis,那么它应该将 'Carmel' 替换为 indianapolis,或者将整个查询字符串“&hos=Carmel”替换为“&hos=Indianapolis”(对于第二种情况),将“?hos=Carmel”替换为“?hos=”印第安纳波利斯”对于第一个场景,

$(".hospitalDropDown").change(function(e){
            var querystring=window.location.search;
            var currentURL=$(location).attr('href');
            if(location.href.indexOf('?') == -1) {
                window.location.href= 'http://mysite/events/Pages/default.aspx'+'?hos='+$(this).val();
                }
                else{

                    window.location.href = ( $(this).val() == "All Hospitals" ) ? 'http://mysitesite/events/Pages/default.aspx': location.href +'&hos='+ $(this).val(); }
    )};

我发现了一些漂亮的代码,使用正则表达式来处理查询字符串,但我不明白正则表达式是如何工作的..我怎样才能使用像这样的东西我的情况如下?

function replaceQueryString(url,param,value) {
    var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i");
    if (url.match(re))
        return url.replace(re,'$1' + param + "=" + value + '$2');
    else
        return url + '&' + param + "=" + value;
}

I have a dropdown which had dual role. User can come to the page(http://mysite/events/Pages/default.aspx) straight and use the dropdown or they can first do a search and the filter the search resuld further by selecting the dropdown.
First Case URL will be like
http://mysite/events/Pages/default.aspx?hos=Carmel and
Second Case URL
http://mysite/events/Pages/default.aspx?kwd=health&type=Events&hos=Carmel
This is what I am doing right now but it behaves weird and does sth like this to url http://mysite.events/Pages/default.aspx?hos=Crown&hos=Carmel

So if user selected carmel from the dropdown the first time and decided he/she wanted to look for Indianapolis, then it should either replace 'Carmel' with indianapolis or replace the whole querstring "&hos=Carmel" with "&hos=Indianapolis" for the second case and "?hos=Carmel" with "?hos=Indianapolis" for the first scenario

$(".hospitalDropDown").change(function(e){
            var querystring=window.location.search;
            var currentURL=$(location).attr('href');
            if(location.href.indexOf('?') == -1) {
                window.location.href= 'http://mysite/events/Pages/default.aspx'+'?hos='+$(this).val();
                }
                else{

                    window.location.href = ( $(this).val() == "All Hospitals" ) ? 'http://mysitesite/events/Pages/default.aspx': location.href +'&hos='+ $(this).val(); }
    )};

I found some nifty code that uses regex to handle the querystring but I don't understand how regex works..How can I use sth like below in my case?

function replaceQueryString(url,param,value) {
    var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i");
    if (url.match(re))
        return url.replace(re,'$1' + param + "=" + value + '$2');
    else
        return url + '&' + param + "=" + value;
}

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

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

发布评论

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

评论(2

把人绕傻吧 2025-01-04 06:20:35

您需要执行以下操作:

function replaceQueryString(url,param,value) {
    var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i");
    if (url.match(re))
        return url.replace(re,'$1' + param + "=" + value + '$2');
    else
        return url + '&' + param + "=" + value;
}

$(".hospitalDropDown").change(function(e) {
   window.location.href = replaceQueryString(window.location.href, 'hos', $(this).val());
)};

您找到的 replaceQueryString 代码段已经为您打包在一个简洁的小函数中,因此您无需了解有关正则表达式的任何信息即可使用它。您需要做的就是向该函数传递您的 url、您尝试更改的参数(“hos”)和新值。

另外,$(location).attr('href') 并不是真正有效的,而且您也没有使用它,所以只需将其取出并坚持使用 window.location 即可。 href

最后,尽管在这种特殊情况下您不需要任何正则表达式知识,但至少学习一些基本的正则表达式语法绝对可以使您受益匪浅。查看 http://www.regular-expressions.info/ 以获得一些很好的解释,并且然后使用像 http://www.regextester.com/ 这样的正则表达式测试器来处理示例并尝试写一些你自己的。

You need to do something like this:

function replaceQueryString(url,param,value) {
    var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i");
    if (url.match(re))
        return url.replace(re,'$1' + param + "=" + value + '$2');
    else
        return url + '&' + param + "=" + value;
}

$(".hospitalDropDown").change(function(e) {
   window.location.href = replaceQueryString(window.location.href, 'hos', $(this).val());
)};

The replaceQueryString snippet you found was already packaged up in a neat little function for you, so you don't need to know anything about regex to use it. All you need to do is pass that function your url, the parameter you are trying to change ("hos") and the new value.

Also, $(location).attr('href') isn't really valid, and you weren't using it anyway, so just take it out and stick with window.location.href.

Finally, although you didn't need any regex knowledge in this particular case, you could definitely benefit from learning at least some basic regex syntax. Take a look at http://www.regular-expressions.info/ for some good explanations, and then use a regex tester like http://www.regextester.com/ to play around with the samples and to try writing some of your own.

流年里的时光 2025-01-04 06:20:35
$(".hospitalDropDown").change(function(e){
    var newhos=($(this).val() == "All Hospitals" ) ? '' : '$1hos=' + $(this).val();
    location.href=location.href.replace(/([\?\&])hos=\w+/,newhos);
});
$(".hospitalDropDown").change(function(e){
    var newhos=($(this).val() == "All Hospitals" ) ? '' : '$1hos=' + $(this).val();
    location.href=location.href.replace(/([\?\&])hos=\w+/,newhos);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文