给定数据库中的 html 解码值如何以输入字段中的编码形式输出
在我的数据库中,我有:
ListItem.title = Hello & Goodbye3
然后在我看来,我有:
ko.mapping.updateFromJS(List.list_items_open, <%= raw @list.list_items.where(:completed => false).to_json(:only => [:id, :title, :completed, :position]) %> );
<ol data-bind='template: { name: "list_item_template", foreach: List.list_items_open}'></ol>
<script type="text/html" id="list_item_template">
<li class="listItem">
<input class="list_item_title" maxlength="250" name="list_item[title]" type="text" data-bind="value: title>
</li>
</script>
这里的问题是输入中的值被呈现为:
Hello & Goodbye3
而不是:
Hello & Goodbye3
关于我应该如何处理这个问题的任何想法?我应该编码为原始吗?谢谢
In my database I have:
ListItem.title = Hello & Goodbye3
Then in my view I have:
ko.mapping.updateFromJS(List.list_items_open, <%= raw @list.list_items.where(:completed => false).to_json(:only => [:id, :title, :completed, :position]) %> );
<ol data-bind='template: { name: "list_item_template", foreach: List.list_items_open}'></ol>
<script type="text/html" id="list_item_template">
<li class="listItem">
<input class="list_item_title" maxlength="250" name="list_item[title]" type="text" data-bind="value: title>
</li>
</script>
Problem here is that the value in the input is being rendered as:
Hello & Goodbye3
And not:
Hello & Goodbye3
Any ideas on how I should be handling this? Were I should be encoding as raw? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您可以控制数据库中的内容,我会说不要对数据库中的文本进行编码。您应该将文本存储为“Hello & Goodbye3”。
请参阅 我们是否应该对特殊字符进行 HTML 编码在将它们存储到数据库之前?以进行更多讨论。
If you have control over the content in the database, I would say don't encode text in the database. You should store your text as "Hello & Goodbye3".
See Should we HTML-encode special characters before storing them in the database? for more discussion.
如果您无法控制数据库中的内容,您可以使用 dependentObservable 来处理编码和解码:
请参阅 http://jsfiddle.net/unklefolk/k8jYN/ 作为一个工作示例。
If you don't have control over the content in the database you could use a dependentObservable which deals with the encoding and decoding:
See http://jsfiddle.net/unklefolk/k8jYN/ for a working example.