以下代码有效:
var r = confirm('Are you sure you want to delete this category?\nDeleting the category will leave child items without a category');
但是,当通过 php 生成相同的代码时,它会传递 Javascript Unexpected token ILLEGAL
错误...帮助?
echo "<script>var r = confirm('Are you sure you want to delete this category?\nDeleting the category will leave child items without a category');</script>";
是否可以在不使用ajax等的情况下做到这一点?我已经知道如何处理弹出窗口中的事件,只是在显示确认框时遇到一些问题。我可以只使用一个表单并获得预期的功能,但我认为确认框会更好。
最佳答案
您需要对新行
进行两次转义,一次用于 JS,一次用于 PHP:
echo "<script>var r = confirm('Are you sure you want to delete this category?\\nDeleting the category will leave child items without a category');</script>";
关于通过 PHP 生成时 Javascript 非法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27581468/