【发布时间】:2016-01-23 16:51:40
【问题描述】:
我的 jquery 代码中有一个 textarea,当用户在 textarea 中键入一些包含换行符的字符串时,它将正确保存在表中。但是当我尝试在编辑页面中显示此字符串时,由于输入导致此 jquery 错误:
SyntaxError: 未终止的字符串文字
这是我的代码:
+ '<textarea placeholder="content" class="form-control " rows="5" type="text" name="InteractiveContent[' + index + '][text]" style="display: inline;" required ><?php echo @$text; ?></textarea>'
我不得不临时 preg_replace 换行以防止出错:
$text = preg_replace("/\r|\n/", "", $text);
但是我的字符串中需要换行符,有什么办法可以防止这个错误或有什么解决方案来处理这个问题吗?我感谢任何帮助..
当我尝试回显包含带有 enter 的字符串的 $text 变量时,我的 jquery 中会发生以下情况:
+ '<textarea placeholder="content" class="form-control " rows="5" type="text" name="InteractiveContent[' + index + '][text]" style="display: inline;" required >this is a text with break line
this is the rest of text after break line
'
以及所有代码:
<script>
jQuery(document).ready(function () {
console.log('hwllo');
var index = 0;
console.log(index);
var addmorehtml = '';
<?php foreach ($interactive_contents as $interactive_content) { ?>
<?php $id = @$interactive_content['InteractiveContent']['id'];
$code = @$interactive_content['InteractiveContent']['code'];
$text = @$interactive_content['InteractiveContent']['text'];
// $text = preg_replace( "/\r|\n/", " ", $text );
?>
addmorehtml += '<div index="'+index+'" id="interactive_content'+ index +'" style="display :inline;">'
+ '<div class="form-group"><input class="form-control " value = "<?php echo @$id; ?>" type="hidden" name="InteractiveContent[' + index + '][id]" style="display: inline; width: 200px;" required /></div>'
+ '<div class="form-group"><span class="red">*</span><input class="form-control " onkeypress="return event.charCode >= 0 && event.charCode <= 57" placeholder="کد محتوای تعاملی" value = "<?php echo @$code; ?>" type="text" name="InteractiveContent[' + index + '][code]" style="display: inline; width: 200px;" /></div>'
+ '<div class="form-group"><span class="red">*</span><textarea placeholder="متن محتوای تعاملی" class="form-control " rows="5" type="text" name="InteractiveContent[' + index + '][text]" style="display: inline;" required >\'<?php echo @$text; ?>\'</textarea><a onclick="$('+"interactive_content"+index+').remove(); " id="del_items_2'+ index +'" href="#"">حذف</a></div></br></div>'
index++;
<?php } ?>
$("#interactive_content").html(addmorehtml);
});
</script>
<a id="addmore" href="#addmore"><span style ="font-size: 15px"><b>+ افزودن محتوای تعاملی </b></span> </a>
【问题讨论】:
-
我们需要查看导致问题的代码。与有换行符的服务器之间发送数据应该没有任何问题
-
这不是一个完整的变量....这是一个连接中间的sn-p。
-
使用代码 linter...会准确地告诉你问题出在哪里
标签: jquery string syntax-error