繁体   English   中英

为什么我的Javascript警报没有显示?

[英]Why is my Javascript alert not being displayed?

基本上,我试图通过PHP调用Javascript警报。 虽然,警报根本不起作用。

这是我的echo语句,可动态创建警报

echo "<script>alert('Uploaded file was not in the correct format\n\nExample of the correct format:\nquestion1,answer1,answer2,answer3,answer4\n
question2,answer1,answer2,answer3,answer4\nquestion3,answer1,answer2,answer3,answer4')</script>";

我发现,如果我将警报更改为此,则可以正常运行:

echo "<script>alert('Uploaded file was not in the correct format')</script>";

我相信换行符存在问题。

我将代码更改为此,但仍然没有运气:

echo "<script>alert('Uploaded file was not in the correct format\\nExample of the correct format:\\nquestion1,answer1,answer2,answer3,answer4\\n
question2,answer1,answer2,answer3,answer4\\nquestion3,answer1,answer2,answer3,answer4')</script>";

我收到以下错误:

SyntaxError: unterminated string literal
[Break On This Error]   

alert('Uploaded file was not in the correct format\nExample of the
------^

createplay.php (line 1, col 6)

有人对为什么这不起作用有任何建议吗? 在网上进行了广泛搜索,找不到解决方案。

提前致谢!

您的\\n字符已被PHP的文字换行符代替。

字符串文字中未转义的文字换行符是JavaScript中的错误。

使用\\\\n将换行符转义序列发送到JavaScript。

尝试这样的事情:

    <?
    echo "<script>alert('Uploaded file was not in the correct format\\n\\nExample of the correct format:\\nquestion1,answer1,answer2,answer3,answer4\\n
   question2,answer1,answer2,answer3,answer4\\nquestion3,answer1,answer2,answer3,answer4')</script>";
    ?>

为了增加昆汀的答案,将单引号和双引号切换也能解决问题吗?

在\\ n和下一个字符之间插入一个空格,它将起作用。 我在您的系统上为您的代码尝试过,并且有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM