基本上,我试图通过 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。
关于php - 为什么我的 Javascript 警报没有显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18405116/