【问题标题】:How to share variables between ejs and page script如何在 ejs 和页面脚本之间共享变量
【发布时间】:2018-12-01 23:00:02
【问题描述】:

在我的页面中,我有一个通过ejs 填充的选择框:

<select multiple class="form-control" id="clienti" style="height: 150px" required>
  <% for(var i=0; i<clienti.length; i++) {%>
    <option ><%= clienti[i]["Cliente"] %></option>
  <% } %>
</select>

我想用这段代码&lt;%= codiceLisa[x]["Codice_Lisa"] %&gt; 在 div 中显示一些信息,x 变量需要是&lt;select&gt; 标记中所选项目的索引。

我已经试过了:

var x = document.getElementById("clienti").selectedIndex;

但似乎 ejs 看不到 x 变量。

【问题讨论】:

    标签: javascript html node.js ejs


    【解决方案1】:

    您应该为option 添加selected 以使用此元素的selectedIndex。 看起来像:

    <option value="example value" selected="">Something</option>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-29
      • 2021-11-03
      • 2018-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多