为什么新行在此 javascript 警报窗口中不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8967722/
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
Why are new lines not working in this javascript alert window?
提问by Radley Sustaire
I see quite a few different issues with the alert window and new lines. Most are that the \n
is considered a new line in PHP rather than getting sent to javascript.
我看到警报窗口和新行有很多不同的问题。大多数是\n
被认为是 PHP 中的一个新行,而不是被发送到 javascript。
In my case, the string is being outputted in a new window showing \n
. I just tried actually writing \n
into an alert box via jsfiddle, and that worked, so it must be my method of doing things...
就我而言,该字符串正在一个新窗口中输出,显示\n
. 我只是尝试\n
通过 jsfiddle实际上写入警报框,并且奏效了,所以这一定是我做事的方法......
Here is the string returned to console. as you can see, the \n is definitely there:
这是返回到控制台的字符串。如您所见, \n 肯定存在:
Username is required\nPassword is required\nEmail is required\nPhone is required\nCardnumber is required
Username is required\nPassword is required\nEmail is required\nPhone is required\nCardnumber is required
However, it shows up like this:
但是,它显示如下:
Why is this happening? I think it may have something to do with the data type, as it is returned from $.ajax
为什么会这样?我认为它可能与数据类型有关,因为它是从$.ajax
if (canAjax && !try_ajax) {
e.preventDefault();
$.ajax({
type: "POST",
url: "mobilesubmit.php",
data: {"use_ajax": true, "formdata": $("#register_form").first().serializeArray()},
success: function(data) {
// This stupid thing should make new lines!
alert(data);
console.log(data);
},
error: function (request, status, error) {
try_ajax = true;
$("#register_form").submit();
}
});
}
回答by Billy Moon
If your console log is showing \n
rather than a new line, then it means the \
is escaped in the original string...
如果您的控制台日志显示\n
而不是新行,则意味着\
在原始字符串中转义了...
Compare
相比
console.log(1,"\n\n");
console.log(2,"\n\n");
Solution
解决方案
use .replace()
to swap your \n
with newline characters
用于与换行符.replace()
交换您的\n
console.log(3,"\n\n".replace(/\n/g,"\n"))
回答by Amit
The most likely cause of this is that your PHP code is escaping the backslash by adding another backslash in front of it. By the time it gets to your JS code, it's actually
最可能的原因是您的 PHP 代码通过在它前面添加另一个反斜杠来转义反斜杠。当它到达你的 JS 代码时,它实际上是
"Username is required\nPassword is required..."
You can inspect the raw response in the network panel of your debugger. If you try to view it in the console, it'll display the formatted output instead of the raw output.
您可以在调试器的网络面板中检查原始响应。如果您尝试在控制台中查看它,它将显示格式化的输出而不是原始输出。
Double-check your method of JSON serialization in your PHP code and make sure it's doing what you expect with the \n.
仔细检查您的 PHP 代码中的 JSON 序列化方法,并确保它使用 \n 执行您期望的操作。
回答by Ajeet Sinha
try adding a space after \n .It should work
尝试在 \n 之后添加一个空格。它应该可以工作
回答by Alan S
IE11 inspect shows the same problem on an asp.net generated hiddenfield - it displays its value as value="test line 1\ntest line 2"
IE11 检查在 asp.net 生成的隐藏字段上显示了相同的问题 - 它将其值显示为 value="test line 1\ntest line 2"
UT actually contains value="test line 1\\ntest line 2"
UT实际上包含 value="test line 1\\ntest line 2"
Using the element.value.replace(/\\n/g,"\n")
on it then displays the correct line spaced string in a javascript alert()
使用element.value.replace(/\\n/g,"\n")
它然后在javascript中显示正确的行间距字符串alert()
Thanks Billy - saved hours of grief
谢谢比利 - 节省了几个小时的悲伤