jQuery 如何在 URL 中插入 javascript 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17734343/
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-08-26 19:51:34  来源:igfitidea点击:

How to insert javascript variables into a URL

javascriptjquery

提问by user1447679

var a = 1;
var b = 2;
var mylink = "http://website.com/page.aspx?list=' + a + '&sublist=' + b + '";

This doesn't work. Is there a simple way to insert these other variables into the url query?

这不起作用。有没有一种简单的方法可以将这些其他变量插入到 url 查询中?

回答by adeneo

By using the right quotes:

通过使用正确的引号:

var a = 1;
var b = 2;
var mylink = "http://website.com/page.aspx?list=" + a + "&sublist=" + b;

If you start a string with doublequotes, it can be ended with doublequotes and can contain singlequotes, same goes for the other way around.

如果你用双引号开始一个字符串,它可以用双引号结束并且可以包含单引号,反之亦然。

回答by Skotee

In modern JavaScript standards we are using ${var}:

在现代 JavaScript 标准中,我们使用${var}

var a = 1;
var b = 2;
var mylink = `http://website.com/page.aspx?list=${a}&sublist=${b}`;