4

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
4

2 回答 2

9

你可以在jade中做到这一点,甚至比在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

此代码块期望gender是从后端传递到视图的变量。

于 2012-11-29T19:54:32.263 回答
3

对于 Jade:这里有一些输入类型和在提交表单后保持选中值的简写,即这样您就可以填充编辑页面...

单选按钮:

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

选择框:

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

option Jazz option Rock option Rap option Dance

复选框:

checked=theAlbum.checkbox

于 2015-06-17T17:28:57.760 回答