使用 jquery 或 javascript 将字符串附加到 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21970975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:07:17  来源:igfitidea点击:

Append string to url using jquery or javascript

javascriptjqueryhtml

提问by user2680614

Is it possible to append a string to url using the body? Something similar to the JSFiddle below.

是否可以使用正文将字符串附加到 url?类似于下面的 JSFiddle。

http://jsfiddle.net/qXs77/

http://jsfiddle.net/qXs77/

<a href="#" onclick="extendSearch('&page=2');return false">hi</a>

I want to append this query to the url: ?q1=iPodTouch&x=79&y=20

我想将此查询附加到网址: ?q1=iPodTouch&x=79&y=20

When the user clicks the link they will be taken to the next page with the attached string above.

当用户单击该链接时,他们将被带到带有上面附加字符串的下一页。

回答by ashbuilds

Updated:

更新

No need to pass it on another function ,

无需将其传递给另一个函数,

Use like this:

像这样使用:

<a href="http://xxxxxx.com/xx-xxxxxx" onclick="location.href = $(this).attr('href')+'?q1=iPodTouch&x=79&y=20';return false">Navigate</a>

回答by Bill Criswell

If you wanted to utilize jQuery more you could try this approach.

如果您想更多地使用 jQuery,您可以尝试这种方法。

$('a[data-page]').on('click', function(e){
  e.preventDefault();
  var page = $(this).data('page');
  location.href += ( location.search.length ? '&' : '?' ) + 'page=' + page;
});

and have the HTML look like:

并让 HTML 看起来像:

<a href="#" data-page="2">Page 2</a>