ejs 如何在 scriptlet 中获得 if-then-else 行为
ejs How do I get if-then-else behavior in scriptlet
在使用 node.js EJS 生成 HTML 时,我无法使用 "else if"
如果我的 scriptlet 标签看起来像:
<% var qtype; %>
<% if (qobj.ansType == 1) { %>
<% qtype = 'multiChoice'; %>
<% } %>
<% else if (qobj.ansType == 0) { %>
<% qtype = 'shortAnswer'; %>
<% } %>
<% else { %>
<% qtype = 'longAnswer'; %>
<% } %>
Type:<%- select_tag("type",
[{"value": 'multiChoice',"text": "Multiple Choice"},
{"value": 'shortAnswer',"text": "Short Answer"},
{"value": "longAnswer","text": "Text Answer"}],
{"value": qtype}) %> // qtype should be set based on if-else statements above
我得到:Unexpected else token in question.ejs while compiling ejs
我正在尝试让 select 下拉列表中的默认项成为基于 qobj.ansType 值的条件。如果 qobj.ansType = 1 则应设置为 multiChoice,如果 qobj.ansType = 0 则应设置为 shortAnswer。
EJS 的文档说我可以在 scriptlet 中使用完整的 javascript 但它看起来像
否则,如果不允许,那么它是否已满 javascript?我不想连续做 3 个 ifs。
您需要在 if
语句的右大括号后立即添加 else
语句。 else
或 else if
不能单独占一行。
尝试:
<% var qtype; %>
<% if (qobj.ansType == 1) { %>
<% qtype = 'multiChoice'; %>
<% } else if (qobj.ansType == 0) { %>
<% qtype = 'shortAnswer'; %>
<% } else { %>
<% qtype = 'longAnswer'; %>
<% } %>
在使用 node.js EJS 生成 HTML 时,我无法使用 "else if" 如果我的 scriptlet 标签看起来像:
<% var qtype; %>
<% if (qobj.ansType == 1) { %>
<% qtype = 'multiChoice'; %>
<% } %>
<% else if (qobj.ansType == 0) { %>
<% qtype = 'shortAnswer'; %>
<% } %>
<% else { %>
<% qtype = 'longAnswer'; %>
<% } %>
Type:<%- select_tag("type",
[{"value": 'multiChoice',"text": "Multiple Choice"},
{"value": 'shortAnswer',"text": "Short Answer"},
{"value": "longAnswer","text": "Text Answer"}],
{"value": qtype}) %> // qtype should be set based on if-else statements above
我得到:Unexpected else token in question.ejs while compiling ejs
我正在尝试让 select 下拉列表中的默认项成为基于 qobj.ansType 值的条件。如果 qobj.ansType = 1 则应设置为 multiChoice,如果 qobj.ansType = 0 则应设置为 shortAnswer。
EJS 的文档说我可以在 scriptlet 中使用完整的 javascript 但它看起来像 否则,如果不允许,那么它是否已满 javascript?我不想连续做 3 个 ifs。
您需要在 if
语句的右大括号后立即添加 else
语句。 else
或 else if
不能单独占一行。
尝试:
<% var qtype; %>
<% if (qobj.ansType == 1) { %>
<% qtype = 'multiChoice'; %>
<% } else if (qobj.ansType == 0) { %>
<% qtype = 'shortAnswer'; %>
<% } else { %>
<% qtype = 'longAnswer'; %>
<% } %>