我在表单上有一个选择选项,加载记录后,我想选择保存的选项。这是代码:
我正在显示表单的Student.hbs文件,行为obj来自student.js路由
student.hbs
<form>
<div class="form-group">
<label for="day">Day</label>
<select name="day" id="day" class="form-control">
<option value="monday" {{{select act.day 'monday'}}}>Monday</option>
<option value="tuesday" {{{select act.day 'tuesday'}}}>Tuesday</option>
<option value="wednesday" {{{select act.day 'wednesday'}}}>Wednesday</option>
<option value="thursday" {{{select act.day 'thursday'}}}>Thursday</option>
<option value="friday" {{{select act.day 'friday'}}}>Friday</option>
<option value="saturday" {{{select act.day 'saturday'}}}>Saturday</option>
<option value="sunday" {{{select act.day 'sunday'}}}>Sunday</option>
</select>
</div>
</form>
student.js
router.get('/view/:actUUID', (req, res) => {
var uuid = req.params.actUUID;
Student.findByPk(uuid).then(act => {
res.render('student', {
act: act
});
});
});
我已经在/handlers/handlebars.js中创建了车把助手,我在其中编写了所有助手功能。
app.js
var exphbs = require('express-handlebars');
var hbsHelpers = exphbs.create({
helpers:require('./handlers/handlebars').helpers,
defaultLayout: 'main',
extname:'.hbs'
});
app.engine('.hbs', hbsHelpers.engine);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
/handlers/handlebars.js
function hbsHelpers(hbs) {
return hbs.create({
helpers: {
select: function (selected, option) {
return (selected == option) ? 'selected="selected"' : '';
}
}
});
}
module.exports = hbsHelpers;
但是当我转到学生页面时,出现以下错误
错误:缺少助手:“选择” 在对象。 (/卷/ Macintosh HD 2 / GitHub已克隆存储库/action-tours/node_modules/handlebars/dist/cjs/handlebars/helpers/helper-missing.js:19:13) 在Object.eval [作为主要对象处](在createFunctionContext(/ Volumes / Macintosh HD 2 / GitHub Cloned Repos / action-tours / node_modules / handlebars / dist / cjs / handlebars / compiler / javascript-compiler.js:254:23), :15:74) 在主目录(/ Volumes / Macintosh HD 2 / GitHub Cloned Repos / action-tours / node_modules / handlebars / dist / cjs / handlebars / runtime.js:175:32) 在ret(/ Volumes / Macintosh HD 2 / GitHub Cloned Repos / action-tours / node_modules / handlebars / dist / cjs / handlebars / runtime.js:178:12) 在ret(/ Volumes / Macintosh HD 2 / GitHub Cloned Repos / action-tours / node_modules / handlebars / dist / cjs / handlebars / compiler / compiler.js:526:21) 在ExpressHandlebars._renderTemplate(/ Volumes / Macintosh HD 2 / GitHub Cloned Repos / action-tours / node_modules / express-handlebars / lib / express-handlebars.js:247:12) 在ExpressHandlebars。 (/卷/ Macintosh HD 2 / GitHub已克隆 Repos / action-tours / node_modules / express-handlebars / lib / express-handlebars.js:173:21)
答案 0 :(得分:0)
因此,似乎不需要在handlebars.js文件中返回hbs.create。
正确的方法是:
app.js
var exphbs = require('express-handlebars’);
.
.
.
var hbs = exphbs.create({
helpers: require('./handlers/handlebars'),
defaultLayout: 'main',
extname:'.hbs'
});
.
.
app.engine('.hbs', hbs.engine);
在/handlers/handlebars.js
module.exports = {
select: function (selected, option) {
return (selected == option) ? 'selected="selected"' : '';
}
};
答案 1 :(得分:0)
在/handlers/handlebars.js
你做这样的事情
module.exports = {
select: function(selected, options){
return options.fn(this).replace( new RegExp(' value=\"' + selected + '\"'), '$& selected="selected"').replace( new RegExp('>' + selected + '</option>'), ' selected="selected"$&');
},
};