简体   繁体   English

如何使用Jade处理可能预先填写的表格?

[英]How can I handle a potentially pre-filled form with Jade?

I have this huge form that only gets pre-filled data if data already exists in a database. 我有这种巨大的形式,如果数据已经存在于数据库中,则只能获取预填充的数据。 Otherwise, none of the text boxes should have the value parameter in them. 否则,所有文本框中都不应包含value参数。 I thought it would just ignore the value parameters if the variable I get data from does not exist. 我以为如果我从中获取数据的变量不存在,它只会忽略value参数。 But instead, I get an error. 但是,相反,我得到了一个错误。

How to I handle this case? 如何处理此案? Will I have to do an if check before each text box in Jade like the following? 我是否需要在Jade中的每个文本框之前进行if检查,如下所示?

if (typeof(prefilled_data) !== 'undefined')
    input.form-control#prevalence(type="text", name="prevalence")
else
    input.form-control#prevalence(type="text", name="prevalence", value=prefilled_data.tb_burden_estimates.prevalence)

While I don't mind doing this (Sublime Text will help with all the copy-pasting), there are quite a few form fields, so it might get ugly. 尽管我不介意这样做(“崇高文本”将对所有复制粘贴有所帮助),但表单字段很多,因此可能会变得很丑陋。 Is there a way to consolidate this into one check somewhere? 有没有办法将其合并到某处的一张支票中?

you seemed to be suggesting that the if statement's were going to be bulky/make the code hard to read if they were there.. my suggestion would be to programmatically create the inputs, there by reducing the if statements to a more manageable number and answering your question about being able to "consolidate this into one check somewhere" 您似乎建议if语句的体积很大/如果在那里,则使代码难以阅读。我的建议是以编程方式创建输入,方法是将if语句减少为更易于管理的数字并回答您关于“可以在某处将其合并为一张支票”的问题

EDIT 编辑

If you are looking to access data in js.. I have been known to use something like: 如果您想访问js中的数据,。我已经知道使用类似的东西:

script(type='text/javascript').
    window.prefilled_data = !{ JSON.stringify(prefilled_data) };

This will allow you then to access the global window.prefilled_data variable to get front end version of your data 然后,您可以访问全局window.prefilled_data变量以获取数据的前端版本

you can do this: 你可以这样做:

- if (typeof(prefilled_data) === 'undefined'){
-   prefilled_data = '';
- }
input.form-control#prevalence(type="text", value=#{prefilled_data})

if prefilled_data is undefined you just set a '' value 如果未定义prefilled_data则只需设置一个''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM