【问题标题】:Ejs inside an html taghtml标签内的Ejs
【发布时间】:2018-07-08 05:22:37
【问题描述】:
  <input type="checkbox" 
         <% if(email.status === 1) { %> checked <% } else { %> <% } %> >

我想根据使用 EJS 的查询结果显示选中或未选中的复选框。但当然 html 认为第一个 %> 是结束标记。有没有办法解决这个问题?谢谢

【问题讨论】:

  • 你所拥有的应该可以工作。你是如何渲染这个页面的?
  • 它不起作用。我正在使用ejs。我已经放了一个截图,所以你可以想象会发生什么
  • 我看不到屏幕截图,但我确实测试了您的代码并且加载正常。我将在下面发布代码
  • 我现在看到了截图。是 IDE 抱怨的问题还是您无法显示该页面?如果 IDE 出现问题,请尝试为您使用的任何 IDE 查找 EJS 插件。

标签: javascript html node.js ejs


【解决方案1】:

以下显示页面。我的猜测是您没有传递导致服务器错误的 email.status 变量。或者你正在尝试使用浏览器来渲染ejs页面,这将无法工作你需要使用视图引擎。

checkbox.ejs

<input type="checkbox" <% if(email.status === 1) { %> checked <% } else { %> <% } %> >

app.js

var express = require('express');
var bodyParser = require('body-parser'); 
var app = express();

app.use(bodyParser.urlencoded({'extended': 'true'})); 
// set the view engine to ejs
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));


app.get('/', function(req, res) {
    res.render("pages/checkbox", {email: {status: 1}});
});

app.listen(8000);
console.log('Server is running on port 8000');
console.log('http://localhost:8000/');

节点 app.js

【讨论】:

    猜你喜欢
    • 2014-12-01
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 2014-07-21
    • 2014-09-30
    • 2017-07-13
    相关资源
    最近更新 更多