为什么新行无法在此javascript警报窗口中运行? [英] Why are new lines not working in this javascript alert window?
问题描述
我看到警报窗口和新行有很多不同的问题。大多数是 \ n
被认为是PHP中的新行而不是发送到javascript。
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.
In在我的情况下,字符串在新窗口中输出,显示 \ n
。我只是尝试通过jsfiddle将 \ n
写入一个警告框,这很有用,所以它一定是我做事的方法......
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肯定在那里:
Here is the string returned to console. as you can see, the \n is definitely there:
用户名是必需的\ n密码是必需的\ nEmail是必需的\ nPhone是必需的\ nCardnumber是必需的
然而,它显示如下:
为什么会这样?我认为它可能与数据类型有关,因为它是从 $返回的.ajax
Why is this happening? I think it may have something to do with the data type, as it is returned from $.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();
}
});
}
推荐答案
如果您的控制台日志正在显示 \ n
而不是新行,则表示 \
在原始字符串中转义...
If your console log is showing \n
rather than a new line, then it means the \
is escaped in the original string...
console.log(1,"\\n\\n");
console.log(2,"\n\n");
解决方案
使用 .replace()
用换行符交换 \ n
console.log(3,"\\n\\n".replace(/\\n/g,"\n"))
这篇关于为什么新行无法在此javascript警报窗口中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!