javascript - rails text_area format for javascript
问题描述
An attribute of type text
needs to be generated within a javascript.
Some records have carriage returns inserted by the user and these need to be passed to javascript in form \n
. However when I place
<%= uc.main_text %>
within the javascript, it returns carriage returns
Salties at bottom...
what happens here I am not certain
when \n
is needed.
Salties at bottom...\n\nwhat happens here I am not certain
From the console calling the attribute returns a proper string
uc.main_text
[...] \r\n\r\n [...]
How can this be transposed for use by the javascript?
解决方案
string = "Salties at bottom...
what \"happens\" here I 'am' not certain"
puts %{var string = "#{escape_javascript(string)}"}
这将打印:
var string = "Salties at bottom...\n\nwhat \"happens\" here I \'am\' not certain"
推荐阅读
- javascript - 在javascript中使用多个文件进行自定义排序
- javascript - 设置输入值等于 div 内容
- swift - Swift:使用子视图后的滚动视图不起作用
- android - 拆分 apk 与 null 崩溃,没有拆分工作正常
- scope - 量角器中的变量范围
- ruby-on-rails - devise_scope no object error, Ruby On Rails
- python - writing the received data to a text file
- mysql - 如何使用消除重复的联接从另一个表中插入一个表
- javascript - CSS div (absolute) inside table th (relative), div is hidden
- c# - C#中条件块后的分号有什么作用?