【问题标题】:Pug JS: How to keep the entered values in input fields instead of clearing the form after validationPug JS:如何将输入的值保留在输入字段中,而不是在验证后清除表单
【发布时间】:2020-03-21 20:10:40
【问题描述】:

我正在使用带有 Pug 模板的 NodeJS Express,并尝试在提交和失效失败后保留输入字段的输入值。它似乎适用于纯 HTML,但不适用于 pug。我知道语法有问题,但我在谷歌上找不到类似的案例。请帮忙。

作品:

<input type="text" name="name" id="name" placeholder="Name" value="<%= typeof name != 'undefined'? name: '' %>

不起作用:

input#name.form-control(type="text" name="name" placeholder="Name" value="<%= typeof name != 'undefined'? name: '' %>")

【问题讨论】:

  • 你的第一个代码 sn-p 没有验证,没有关闭 &gt;

标签: javascript html node.js pug


【解决方案1】:

您可以通过以下方式进行:

  • 在 pug 文件中:

    input(type="text", name="q", value= q)

  • 在js文件中:

    app.get('/users/search', (req, res) => {
        var q = req.query.q;
        // something
        res.render('users/index', {
            q: q
        });
    })
    

    希望有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-18
    • 2023-03-14
    • 2022-11-17
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    相关资源
    最近更新 更多