永远不应该存在两个具有相同名称的查询字符串 [英] never two query string with the same name should exists

查看:80
本文介绍了永远不应该存在两个具有相同名称的查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用两个锚标签,href属性包含查询字符串.. 1st包含href,如default.aspx?cat = paper,2nd包含href,如default.aspx?subcat = pen



i使用锚标签绑定onclick事件..



假设当用户点击第一个锚标记网址时会像default.aspx一样?cat = paper



我的问题从这里开始..



当用户点击第二个网址然后javascript代码应该检查对于url是subcat查询字符串已经退出,如果是,然后用新id更新现有的subcat,如果没有,那么只需添加url,因为它实际上我希望永远不会有两个具有相同名称的查询字符串应该存在于url中(没有重复查询字符串)。

i am using two anchor tags with href property contains query strings.. 1st contains href like default.aspx?cat=paper and 2nd contains href like default.aspx?subcat=pen

i have binded onclick event with anchor tags..

suppose when user clicks on 1st anchor tag url will be like default.aspx?cat=paper

my problem starts from here..

when user clicks on 2nd url then javascript code should check for url that is subcat query string already exits if yes then update the existing subcat with new id and if not then simply add url as it is.. Actually i want never two query string with the same name should exists in the url (no duplicacy of query strings).

推荐答案

您可以执行以下任务。



步骤1:从第1个Url获取查询字符串



You can do above task as below.

Step 1 : Get the query string from the 1st Url

// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}





例如:



href =http://www.example.com/default.aspx?cat = paper



以上代码将返回:





As an example:

href="http://www.example.com/default.aspx?cat=paper

Above code will return :

{
    "cat"    : "paper",
}





那么你可以得到如下价值:





Then you can get that value is as below:

var cat = getUrlVars()["cat"];





步骤2:当用户点击第二个网址时附加上面的值



欲了解更多信息:



jquery从URL获取查询字符串





我希望这会对你有所帮助。



Step 2: Append above value when user clicks the 2nd url

For more info:

jquery get querystring from URL


I hope this will help to you.


这篇关于永远不应该存在两个具有相同名称的查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆