使用jQuery构造带参数的URL [英] Constructing a URL with parameters using jQuery

查看:128
本文介绍了使用jQuery构造带参数的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储在全局变量中的以下URL:

I have for example the following URL stored in a global variable:

var myUrl = "http://mydomain.com/something?row=1";

然后一个函数必须添加另外一个名为 column 的参数。该函数如何使用jQuery将参数添加到预先存在的URL字符串?

Then a function has to add let's say another parameter called "column". How would that function add parameters to a pre-existing URL string using jQuery?

预期生成的字符串示例:

Example of the expected generated string:

"http://mydomain.com/something?row=1&column=9"

问题是 myUrl 也可能只是:

var myUrl = "http://mydomain.com/something";

(请注意,没有预先存在的参数)

(Notice that there are not pre-existing parameters)

推荐答案

查看jQuery函数.param(),这应该可以解决问题。

Check out the jQuery function .param(), that should do the trick.

http://api.jquery.com/jQuery.param/

然后,您可以创建一个函数,将.param()生成的字符串附加到URL。

You can then just create a function which appends the string generated by .param() to a url.

这篇关于使用jQuery构造带参数的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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