问题
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:
This seems to be the ONLY valid version of unchecked:
> <input type="checkbox" name="vehicle" value="Bike">
While this is 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">
instead of this:
<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?
回答1:
You can use:
input(type="checkbox", name="completed", checked=(true===false ? "checked" : undefined))
回答2:
No need to specify the values:
input(type="checkbox", name="completed", checked=(condition))
If condition is false, there is no checked attribute will added.
回答3:
Another way to do the same thing is by 'if' condition:
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.
回答4:
you can try like this
option(value='man' selected = profile.sex ==='man') man
option(value='female' selected = profile.sex ==='female') female
来源:https://stackoverflow.com/questions/14147772/jade-checkbox-checked-attribute-unchecked-based-on-conditional-if