Display data inside input value using Jade

喜你入骨 提交于 2019-11-29 13:19:00

问题


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

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:

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 input by doing something like

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

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

// 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
            });
        });
    };
};

回答1:


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

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




回答2:


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

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

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




回答3:


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



来源:https://stackoverflow.com/questions/21300877/display-data-inside-input-value-using-jade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!