jquery修改url获取参数 [英] jquery modify url get parameters

查看:126
本文介绍了jquery修改url获取参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的网址:

  http://google.de/test.php?a=b& c = d&e = f 

我知道只需修改一个GET参数,如下所示: ( c 是h而不是d)

  http ://google.de/test.php?a = b& c = h& e = f 

并重定向到新的网址。所有其他的GET参数应该保持不变。



我该怎么做?

解决方案



然而,这里是一个基于常规的字符串替换解决方案表达式,如果你需要更简单的快速修复。

  var url =http://my.com/page?x=y& ; Z = 1&安培; W = 34\" ; 

var regEx = /([?&] z)=([^#&] *)/ g;
var newurl = url.replace(regEx,'$ 1 = newValue');

这里我用'newVlaue'替换查询字符串键'z'。

看看这个小提琴: http://jsfiddle.net/BuddhiP/ PkNsx


I got a URL like this:

http://google.de/test.php?a=b&c=d&e=f

I know got to modify just one of the GET params like this: (c is "h" instead of "d")

http://google.de/test.php?a=b&c=h&e=f

and redirect to the new url. All other GET params should stay the same.

How am I going to do this?

解决方案

I agree its best to use a library for this purpose as mentioned in other answers.

However, here is a string replacement solution based on regular expressions if you need a simpler quick fix.

var url = "http://my.com/page?x=y&z=1&w=34";

var regEx = /([?&]z)=([^#&]*)/g;
var newurl = url.replace(regEx, '$1=newValue');

Here I'm replacing the query string key 'z' with 'newVlaue'

Have a look at this fiddle: http://jsfiddle.net/BuddhiP/PkNsx

这篇关于jquery修改url获取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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