【发布时间】:2018-07-23 08:32:27
【问题描述】:
我的表单中有一个单选按钮。如果选择了true,我想显示一个附加字段。如果false,我想隐藏附加字段。我已经尝试了几种在 SO 上找到的可能解决方案,但似乎都没有奏效。
以我的形式:
%fieldset
%legend
Visa & Passport
.form-group
= f.input :work_visa, as: :radio_buttons, label: "Do you have a work visa?", collection: [['Yes', true], ['No', false]], checked: true, item_wrapper_class: 'radio-inline', wrapper_html: { id: 'work-visa'}
= f.input :visa_exp_date, label: 'Visa Expiration Date', item_wrapper_class: 'form-inline', class: 'col-xs-4', wrapper_html: { id: 'work-visa-exp'}
在页面底部:
:javascript
$('#work-visa-exp').show();
$('input[type=radio]').on("change", function(){
if($(this).prop('id') == "work-visa") {
$('#work-visa-exp').toggle();
}
});
我很确定错误出在我的 JS 中。如果我注释掉除$('#work-visa-exp').show(); 之外的所有内容并将.show() 更改为.hide(),该字段将按预期隐藏。
【问题讨论】:
标签: javascript jquery ruby-on-rails haml simple-form