Javascript 玉复选框选中属性未选中基于条件(如果)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14147772/
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-08-24 15:50:31  来源:igfitidea点击:

Jade checkbox checked attribute unchecked based on conditional (if)

javascriptnode.jsexpresspug

提问by jstevens13

How do I get jade to render the checked attribute of a checkbox based on a conditional? Like these two versions of the HTML checkbox tag:

如何让 jade 根据条件呈现复选框的选中属性?像这两个版本的 HTML 复选框标签:

This seems to be the ONLY valid version of unchecked:

这似乎是 unchecked 的唯一有效版本:

>  <input type="checkbox" name="vehicle" value="Bike">
>  <input type="checkbox" name="vehicle" value="Bike">

While this is checked:

虽然这被检查:

> <input type="checkbox" name="vehicle" value="Car" checked="checked">
> <input type="checkbox" name="vehicle" value="Car" checked="checked">

Here is what I've tried so far:

这是我迄今为止尝试过的:

This Jade is fine:

这个玉很好:

    input(type="checkbox", name="completed", checked=(true===true ? "checked" : "")).checkbox

because it renders this:

因为它呈现这个:

<input type="checkbox" name="completed" checked="checked" class="checkbox">

but this Jade is not fine:

但这玉不好:

    input(type="checkbox", name="completed", checked=(false===true ? "checked" : "")).checkbox

because it renders this:

因为它呈现这个:

<input type="checkbox" name="completed" checked="" class="checkbox">

<input type="checkbox" name="completed" checked="" class="checkbox">

instead of this:

而不是这个:

<input type="checkbox" name="completed" class="checkbox">

<input type="checkbox" name="completed" class="checkbox">

How do I get Jade to render the entire checked attribute instead of just the value of the checked attibute?

如何让 Jade 呈现整个已检查的属性,而不仅仅是已检查属性的值?

回答by Michael Yin

You can use:

您可以使用:

input(type="checkbox", name="completed", checked=(true===false ? "checked" : undefined))

回答by Vladimir Kalinichev

No need to specify the values:

无需指定值:

input(type="checkbox", name="completed", checked=(condition))

If condition is false, there is no checked attribute will added.

如果条件为假,则不会添加已选中的属性。

回答by Mahima Agrawal

Another way to do the same thing is by 'if' condition:

做同样事情的另一种方法是通过“if”条件:

if(#{data.refrigerated} == '1')
   input(type='checkbox', name='refrigerated', checked)
else
   input(type='checkbox', name='refrigerated')

When condition will be true, You will get checked checkbox on DOM otherwise you will get unchecked checkbox.

当条件为真时,您将在 DOM 上选中复选框,否则将取消选中复选框。

回答by Gavin Gong

you can try like this

你可以这样试试

  option(value='man' selected = profile.sex ==='man') man
  option(value='female' selected = profile.sex ==='female') female