Jade radio button checked condition

I have form in Jade file, and I want it to be pre-filled with some values that come from backend.In the form, there are radio buttons for user gender.

input(type="radio", name="gender_filter", value="1") Male
input(type="radio", name="gender_filter", value="0") Female

Now I also have this variable gender_param , and I want the corresponding button to be selected when page loads. In PHP , I could do this:

<input type="radio" name="gender_filter", value="1" <?php echo ($gender_param==1)?"checked":''; ?>>

Is there the corresponding syntax for Jade? Or I need to write it in the long way with line duplicates like

- if gender_param==1
   input(type="radio", name="gender_filter", value="1", checked) Male
- else
   input(type="radio", name="gender_filter", value="1") Male

You can do this in jade even simpler than in PHP:

input(type="radio", name="gender_filter", value="1", checked=gender=="male")
| Male
input(type="radio", name="gender_filter", value="0", checked=gender=="female")
| Female

This code block expects gender to be a variable passed from the backend to the view.


For Jade: here are a few input types and the shorthand for keeping the checked value once submitting the form, ie so you can populate an edit page...

Radio Button:

input(type="radio" name="stars" value='1' id='stars' checked=theAlbum.rating=='1')

Select box:

option(selected = theAlbum.genre) #{theAlbum.genre}

option Jazz option Rock option Rap option Dance

Checkbox:

checked=theAlbum.checkbox

链接地址: http://www.djcxy.com/p/56044.html

上一篇: 如何使用ASP.Net设置无线电输入标记的检查值?

下一篇: Jade单选按钮检查条件