簡體   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