【问题标题】:How to render radion button value while updating a record如何在更新记录时呈现单选按钮值
【发布时间】:2020-08-07 01:54:20
【问题描述】:

在更新节点 js 中的记录时,我能够呈现文本输入字段的值,但在更新表单中,单选按钮和文本区域的值不会出现。任何人都可以让我知道如何实现它。 我正在使用 hbs 模板。

<div class="form-group row">
    <label for="inputPassword3" class="col-sm-5 col-form-label" style="color: #2874a6 ;">Owner Name</label>
    <div class="col-sm-6">
        <input type="email" class="form-control" name="owner" value={{product.owner}}>
    </div>
</div>
<div class="form-group row">
    <label for="inputPassword3" class="col-sm-5 col-form-label" style="color: #2874a6 ;">Description</label>
    <div class="col-sm-6">
        <textarea class="form-control" name="description" rows="2" value={{product.description}}></textarea>
    </div>
</div>

<div class="form-group row">
    <label for="inputPassword3" class="col-sm-5 col-form-label" style="color: #2874a6 ;">Summary column format</label>
    <div class="col-sm-6">
        <div class="radio-inline">
            <input class="form-check-input" type="radio" name="taskDone" value="Yes" onClick="changeresult();">if({{product.team}}=="Yes"){ %> checked
            <%} %>> Yes </input>

        </div>
        <div class="radio-inline">
            <input class="form-check-input" type="radio" name="taskDone" value="No" onClick="changeresult();">if({{product.taskDone}}=="Yes"){ %> checked
            <%} %>> Yes </input>

        </div>
    </div>
</div>

textarea 字段和单选按钮的值未出现。 提前感谢您的帮助

【问题讨论】:

  • 您确定您正确使用了帮助程序吗? The docs suggest 一种看起来更像这样的方法:{{#if product.team == "Yes"}} checked {{/if}}。您的示例看起来更像EJS 而不是handlebars。我对车把只略知一二,但在阅读文档一段时间后,这是一个最好的猜测。
  • 我根据你的建议编辑了我的代码 Yes 但在第 125 行出现错误解析错误:..." {{#if ticket.team == “是”}} 检查 { -----------------------^ 期待 'CLOSE_RAW_BLOCK'、'CLOSE'、'CLOSE_UNESCAPED'、'OPEN_SEXPR'、'CLOSE_SEXPR ','ID','OPEN_BLOCK_PARAMS','STRING','NUMBER','BOOLEAN','UNDEFINED','NULL','DATA','SEP',得到'EQUALS'

标签: javascript mysql node.js express handlebars.js


【解决方案1】:

我不经常与handlebars 合作,因此您可能需要进行更多研究。

看起来可能是{{if}}doesn't support comparisons, only existence and booleans。因此,根据一些研究,您有几个选择:

选项 1:布尔值
您可以将值作为布尔值提供给模板。因此,不是“是”、“否”,而是让中间件(或者更好地更新您的数据库模式并相应地存储它们)将它们提供为truefalse。然后你可以使用:

{{#if product.team}} checked {{/if}}

选项 2:注册助手

在您的 javascript 代码中注册帮助程序:

Handlebars.registerHelper('ifEquals', function(arg1, arg2, options) {
    return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
});

在模板中使用:

{{#ifEquals product.team "Yes"}} checked {{/ifEquals}}

你可以read more via this question

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 2021-04-22
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多