我有一个带有一些字段和单选按钮的表单,我想根据单选按钮显示不同的内容。这是我对Course.rb
的_form.inputs
= f.input :name, input_html: {class: 'span' }
= f.input :cod, input_html: {class: 'span' }
= f.association :type
:as => :radio_buttons,
:required => true
我也有型号Type.rb,它有id,名字(1 - 本科生和2 - 毕业生)。我想根据单选按钮显示不同的内容。但是,我不知道我该怎么做。我可以使用JQuery吗?
例如,如果用户选择毕业生,我想要显示一些字段/链接/ div。如果他或她选择本科,我将展示不同的东西。
我试图输入一个if(@ course.type_id == 1),但是id不起作用,因为我不知道我得到了哪个号码。
谢谢!
答案 0 :(得分:0)
您必须使用javascript根据所选的单选按钮操作DOM。
示例,在coffeescript中:
jQuery ->
$("input[name='type_id']").change ->
if this.val() == 1
// show some information somewhere
else if this.val() == 2
// show another information somewhere
如果我能改进答案,请告诉我。)
答案 1 :(得分:0)
selected_val = $('input[name=radioName]:checked', '#myFormid').val();
check if selected_val
//do something