使用jQuery更改url查询字符串值 [英] Change url query string value using jQuery
问题描述
我有一个示例网址,例如:
I have an example URL like:
http://domain.com/Documents/?page=1&name=Dave& date = 2011-01-01
查询字符串包含当前页码和另外两个过滤器(名称和日期)。
The query string contains the current page number and two additional filters (name and date).
使用以下URL解析器: https://github.com/allmarkedup / purl 我能够访问URL的某些部分,例如页码。
Using the following URL parser: https://github.com/allmarkedup/purl I am able to access certain parts of the URL such as just the page number.
我正在尝试为用户创建一种方式能够在文本框中键入数字,然后加载该页码,同时保持所有其他查询字符串不变。
I'm trying to create a way for a user to be able to type a number into a textbox and then load that page number whilst keeping all the other query strings intact.
$(document).ready(function () {
$('.pageNum').live('keyup', function (e) {
e.preventDefault();
if (e.which == 13) {
var currentUrl = window.location.href;
var parsedUrl = $.url(currentUrl);
var currentPageNum = parsedUrl.param('page');
var newPageNum = $(this).val();
var newUrl = //
window.location.href = newUrl;
}
});
});
因此当用户点击pageNum文本框上的返回时,它将获取当前页面的URL,解析它,然后找出当前的页码,然后我需要一种方法用文本框中的新值替换页码的值来创建一个新的URL,然后最后使用这个新的URL刷新页面。
So when a user hits return on the pageNum textbox, it will get the current page url, parse it, then find out the current page number and then I need a way to replace the value of the page number with the new value in the textbox to create a new url, and then finally refresh the page using this new url.
是否可以更改参数值然后重新添加?
Is it possible to change the param value and then add it back in?
注意:其他参数可以什么都行,所以我不能用新的页码手动将它们添加到路径名上!
推荐答案
purls没有参数的 $。params()
将为您提供一个密钥 - 参数的值对象。
purls $.params()
used without a parameter will give you a key-value object of the parameters.
jQuerys $ .param()
将从提供的对象/数组构建一个查询字符串。
jQuerys $.param()
will build a querystring from the supplied object/array.
var params = parsedUrl.param();
delete params["page"];
var newUrl = "?page=" + $(this).val() + "&" + $.param(params);
更新
我不知道为什么我在这里使用 delete
...
var params = parsedUrl.param();
params["page"] = $(this).val();
var newUrl = "?" + $.param(params);
这篇关于使用jQuery更改url查询字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!