简体   繁体   English

如何在JavaScript变量中设置ckeditor html数据

[英]How to set ckeditor html data in a javascript variable

I am having a problem with ckeditor data. 我的ckeditor数据有问题。 I have some ckeditor html data saved in my data base as a description. 我在数据库中保存了一些ckeditor html数据作为说明。 So when i need to update that description i need to set the data from the database and show it in the textarea in order to show user what was it. 因此,当我需要更新该描述时,我需要设置数据库中的数据并将其显示在文本区域中,以便向用户显示它是什么。 I'm using laravel 5.3. 我正在使用laravel 5.3。 In order to do this i have tried this 为了做到这一点,我已经尝试过

var old_description = '<?php echo $Product->description;?>';
$('#detail').val(old_description);

But this is giving the following error 但这给出了以下错误

Uncaught SyntaxError: Invalid or unexpected token 未捕获的SyntaxError:无效或意外的令牌 在此处输入图片说明

What is the problem and what should i do? 有什么问题,我该怎么办?

The description you are passing contains multiple lines witch produces a javascript error. 您传递的描述包含多行,可能会产生javascript错误。 Try encoding the variable when echoing. 在回显时尝试对变量进行编码。

<?php echo json_encode($Product->description);?>

You should just use json_encode() from http://www.php.net/manual/en/function.json-encode.php 您应该只使用http://www.php.net/manual/zh/function.json-encode.php中的 json_encode()

var old_description = '<?php echo json_encode($Product->description); ?>';

It takes care of converting < / >, escaping any other special characters as necessary, preserve spaces, etc. 它负责转换</>,根据需要转义任何其他特殊字符,保留空格等。

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

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