繁体   English   中英

未终止的字符串文字Javascript

[英]unterminated string literal Javascript

我在php中使用以下代码:

PHP:

 $suggested_sentence[0] = "Hello, how are you?";
  echo $suggested_sentence[0] . ' <input type="image" src="button.png" 
  onclick = update_textarea('. $textareacount.','. "'".$suggested_sentence[0]."')/>";

JavaScript函数:

function update_textarea(count, new_sentence) {
    document.getElementById('sentence' + count).value = new_sentence;
}

但是,当我按下按钮时,出现错误“未终止的字符串文字,但是如果我更改$suggested_sentence[0] = "Hello"的值,它将正常工作。

那我该怎么办?

PHP执行后检查文件,以确保您的按钮看起来像这样...

<input type="image" src="button.png" onclick="update_textarea(1, 'Hello, how are you?');"/>

请注意,onclick事件用双引号引起来。

您可以像这样调整示例:

 $suggested_sentence[0] = "Hello, how are you?";
  echo $suggested_sentence[0] . ' <input type="image" src="button.png" 
  onclick="update_textarea('. $textareacount.', '. "'" . $suggested_sentence[0]."'" . ')"/>';

HTML编码 $suggested_sentence[0]因为它可能包含破坏javascript文字的字符(例如'因此请确保为此将quote_styleENT_QUOTES )。

暂无
暂无

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

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