为什么我的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>";
?>
要补充Quentin的答案,切换单引号和双引号是否也能解决问题?
在''n和下一个字符之间放一个空格,它就会起作用。我在我的系统上为您的代码尝试过,它在上运行
相关文章:
- 我希望只有在我滚动页面后才能显示我的返回页首图标
- 希望在单击href链接时显示我的广告
- 我想隐藏/显示我的SVG元素,同时在anguarJS中使用它
- 为什么我的html弹出窗口显示我的函数的文本,而不是运行它们
- 我怎样才能显示我的子数组解唱 Angular js
- 在谷歌地图上显示我的位置
- 如何在谷歌地图上显示我的数据库地址,不带lat和long
- 为什么浏览器不't显示我的日历
- ng-repeat / ng-bind 不会显示我的数据
- 我无法使用 d3.js 在每月数据中显示我的 x 轴
- 为什么我的 onclick 事件不显示我的 JSON 文件
- Firebug 不显示我的 JavaScript?找不到错误
- 我只想在警报中打印数据,但每当它提醒我 [对象对象] 但我希望它会显示我的表数据行
- var.push 并在 HTML 页面中显示我的新临时信息
- 为什么IE无法正确显示我的HttpHandler Web应用程序中的特殊符号
- 谷歌地图没有显示我的位置
- 如何使用 Jquery 在段落中以纯文本形式显示我的数据,该段落以 JSON 文本包装
- JavaScript代码很好,但网页预览只显示我的html工作
- 谷歌地图API v3:我在热图上显示我的数据时遇到问题
- 摘要循环显示我的数组没有变化(视图不会刷新)