JQuery SyntaxError:由于textarea中的断行而未终止的字符串文字

时间:2016-01-23 16:51:40

标签: jquery string syntax-error

我的jquery代码中有一个textarea,当用户在textarea中键入一些包含断行的字符串时,它将正确保存在表中。但是当我尝试在编辑页面中显示此字符串时,由于输入:

,它会导致此jquery错误

SyntaxError:unterminated string literal

这是我的代码:

+ '<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(&#34; / \ r | \ n /&#34;,&#34;&#34;,$ text);

但是我需要在我的字符串中使用breake行,是否有任何方法可以防止此错误或任何解决方案来处理此问题?我很感激任何帮助..

这是我的jquery中发生的事情,当我尝试回复包含输入字符串的$ text变量时:

+ '<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

&#39;

和所有代码:

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

2 个答案:

答案 0 :(得分:0)

问题是'内有'

+ '<textarea  
    placeholder="content"
    class="form-control"
    rows="5"
    type="text" 
    name="InteractiveContent[' + index + '][text]" // error is here
    style="display: inline;"
    required 
  >
    this is a text with break line
    this is the rest of text after break line
  </textarea>'

您需要使用\',因此解析器不会认为您的字符串在那里结束:

+ '<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
  </textarea>'

答案 1 :(得分:0)

看到这个plunker可能对你有帮助

var str = new String(document.querySelector(".form-control").value);
var newstr = str.replace(/\n/g, "<br/>");

https://plnkr.co/edit/78CCoOsFWo8iEuE7DRz3?p=preview