javascript 使用 Jade 在输入值中显示数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21300877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 20:36:27  来源:igfitidea点击:

Display data inside input value using Jade

javascriptnode.jsexpresspug

提问by Trevan Hetzel

I'm fairly new to Jade and am wanting to display some outputted data as the valuevalue of a text input. Like this:

我对 Jade 还很陌生,我想将一些输出数据显示为valuetext的值input。像这样:

input(type="text", name="date", value="THISRIGHTHURR")

But only the value needs to be viewpost.date. I've tried multiple ways and none seem to work:

但只有值需要是viewpost.date. 我尝试了多种方法,但似乎都不起作用:

input(type="text", name="date", value=viewpost.date) // doesn't work
input(type="text", name="date", value=.=viewpost.date) // doesn't work
input(type="text", name="date", value=".=viewpost.date") // doesn't work

I of course can get it to work outside of an inputby doing something like

我当然可以input通过做类似的事情让它在 an 之外工作

each post, i in viewpost
  h1.=post.date

Am I supposed to loop through in the inputsomehow too? This is the JS (using Node and Express) that's outputting my viewpostvariable.

我也应该以input某种方式循环吗?这是输出我的viewpost变量的 JS(使用 Node 和 Express)。

// render show post view
exports.viewpost = function(db) {
    return function(req, res) {
        var id = req.params.id;

        collection.find({ "_id": new BSON.ObjectID(id) }, function (err, data) {
            res.render("viewpost", {
                "viewpost" : data
            });
        });
    };
};

回答by asym

Pug 0.1.0 (Jade 2.x) removed support for interpolation in attributes, so this works now:

Pug 0.1.0 (Jade 2.x) 移除了对属性插值的支持,所以现在可以使用了:

input(type="text", name="date", value=viewpost.date)

See https://github.com/pugjs/pug/issues/2305

https://github.com/pugjs/pug/issues/2305

回答by Munim

You can try enclosing the variable in #{}to output it:

您可以尝试将变量括起来#{}以输出它:

input(type="text", name="date", value="#{viewpost.date}")

input(type="text", name="date", value="#{viewpost.date}")

回答by tbushman

I realize this is old news, but I found that neither of these worked, and ended up doing it like this, for anyone stumped like I was:

我意识到这是旧消息,但我发现这些都没有奏效,最终这样做了,因为任何人都像我一样难倒:

input(type="text", placeholder=data.string)

and then in a script:

然后在脚本中:

$(document).on('focus', 'input', function(){
    var text = $(this).attr('placeholder');
    $(this).val(text);
})

thx

谢谢