javascript 或 jquery:在一个警报中显示多个变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18093782/
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
javascript or jquery: show multiple variables in one alert
提问by secr
Alert command assumes this structure:
警报命令采用以下结构:
alert (variable)
How to show multiple variables in a single alert?
如何在单个警报中显示多个变量?
回答by nnnnnn
Alert command assumes this structure:
alert (variable)
警报命令采用以下结构:
alert (variable)
No, alert()
assumes this structure:
不,alert()
假设这种结构:
alert(some expression)
...where "some expression" is pretty much any JavaScript expression - if the expression is not a string it will be converted (though in some cases, e.g., for some objects the result might not be very meaningful).
...其中“某些表达式”几乎是任何 JavaScript 表达式 - 如果表达式不是字符串,它将被转换(尽管在某些情况下,例如,对于某些对象,结果可能不是很有意义)。
So:
所以:
alert(variable);
alert("string literal");
alert(variable1 + variable2 + variable3);
alert(variable1 + ", " + variable2);
alert(resultOfFunctionCall());
alert([1,2,3]);
alert(whatever() + "else" + you.can.think + "of");
Or even:
甚至:
alert(); // displays "undefined"
Note that if you are trying to debug your code you are better off using console.log()
than alert()
. If you are trying to produce a dynamic message to show the user just concatenate variables as needed, e.g.:
需要注意的是,如果你想调试你的代码,你有过使用更好的console.log()
比alert()
。如果您尝试生成动态消息以显示用户只需根据需要连接变量,例如:
alert("Hello there " + name + ". Welcome.");
回答by Raptor
Do you mean this :
你的意思是:
alert (variable1 + ', ' + variable2);
No jQuery is required in this case.
在这种情况下不需要 jQuery。
回答by Harry
The below is how to do it:
下面是如何做到这一点:
var a = "Hello";
var b = "World!";
alert(a + b);
回答by Erik P
This works for me:
这对我有用:
window.alert = function (native) {
return function (str) {
var argsArray = Array.prototype.slice.call(arguments);
var s = "";
for (var i = 0; i < argsArray.length; i++) {
msg = argsArray[i];
if (typeof (msg) == 'object') msg = JSON.stringify(msg);
s += msg;
if (i < (argsArray.length - 1)) s += ', ';
}
native(s);
}
}(window.alert);
Try this:
尝试这个:
alert("This", "That", {"this":"that"});
警报(“这个”,“那个”,{“这个”:“那个”});