【问题标题】:Set input tag value in Underscore template在下划线模板中设置输入标签值
【发布时间】:2026-02-05 14:05:01
【问题描述】:

我正在尝试从传递给它的 javascript 对象中设置输入标记值。

假设 item 具有这些值 item = ['item one', 'item two']

<input type="text" 
        name="choice" 
        val=<%= item[0] %>
        data_id=<%= item[1] %>/>

我希望这样的输出

<input type="text" 
        name="choice" 
        val="item one"
        data_id="item two"/>

但实际生成的html是:

   <input type="text" 
        name="choice" 
        val="item"
        "one"
        data_id="item" />

我的代码有什么问题?

【问题讨论】:

  • JavaScript 是什么样的?你为什么不在模板中引用你的属性?
  • 它就像一个魅力。引用属性后。谢谢

标签: javascript templates input underscore.js underscore.js-templating


【解决方案1】:

根据mu is too short

cmets,我引用了模板中的属性,效果很好。

<input type="text" 
        name="choice" 
        val="<%= item[0] %>"
        data_id="<%= item[1] %>"/>

【讨论】: