如何使用查询参数在jQuery中生成url

How to make url in jQuery using query parameter?

本文关键字:url jQuery 何使用 查询 参数      更新时间:2024-03-25

我正在尝试使用这个插件:https://github.com/tombonner/jurlp.

我想使用参数制作url。我们可以使用这个插件吗?

这是我的代码

// first case
var a = 'http://abc/deappsl/index.html?word=abc';
var abc = {
  test:"lo"
}
// second case
var a = 'http://abc/deappsl/index.html';
var abc = {
  test:"lo",
  p:'er'
}
// expected output
//first
//http://abc/deappsl/index.html?word=abc&test=lo
//second
//http://abc/deappsl/index.html?test=lo&p=er

http://plnkr.co/edit/IEnUPyGAgCCCPeII0xh3?p=preview

我有两箱

  • 首先,在url中我已经有了"?",并添加了参数,它使用"&"给出了预期的结果
  • 第二,如果url不包含"?",则添加带有"?"answers"&"的param

我们可以用这个插件制作url吗?

有什么更新吗?

您可以使用$.jurlp 解析原始URL

var url = $.jurlp(a);

然后可以使用.query()方法更新查询参数。

url.query(abc);

然后查看新的URL

console.log(url.href);

演示