Javascript 如何在下划线模板中存储JSON数据?
我想在下划线模板中存储JSON对象或字符串Javascript 如何在下划线模板中存储JSON数据?,javascript,jquery,json,templates,underscore.js,Javascript,Jquery,Json,Templates,Underscore.js,我想在下划线模板中存储JSON对象或字符串 <script id="report_field_template" type="text/template"> <div data-options="<%= options %>" role="<%- role %>" title="<%- title %>" class="ui-widget-content ui-corner-all report_field">
<script id="report_field_template" type="text/template">
<div data-options="<%= options %>" role="<%- role %>" title="<%- title %>" class="ui-widget-content ui-corner-all report_field">
<p><%- field_text %></p>
</div>
</script>
如果我将“选项”作为JSON字符串或JSON对象传递,上述操作将不起作用。有什么想法吗?你可以试着通过
{options: JSON.stringify(options)}
模板:)之后,您需要在阅读时对其进行解析。技巧是在处理html5数据时使用单引号-存储JSON数据的属性:
<script id="report_field_template" type="text/template">
<div data-options='<%= options %>' role="<%- role %>" title="<%- title %>" class="ui-widget-content ui-corner-all report_field">
<p><%- field_text %></p>
</div>
</script>
请注意,用于保存数据选项值的单引号,而不是双引号。我会确保您的选项中没有任何双引号。您可能需要使用