Rails Forms : change form fields from previously selected fields

时光毁灭记忆、已成空白 提交于 2019-12-08 20:04:30

First of all you can't place id on inputs, because you will end up with two inputs with same and jQuery will only find the first one. Secondly, you need to use $(this) inside your handler.

You need sth in line:

<%= f.input :ration_card ,as: :radio_buttons,collection: ["Yes","No"], wrapper_html: {id: "rationcard"} %>
<%= f.input :rationcardNum , label: "Ration Card No." ,wrapper_html: {id: "rationcard_no"} %>

$(function(){
    var toggle_rationcardNum = function(visible) {
        if (visible){
            $("#rationcard_no").show();
        } else {
            $("#rationcard_no").hide();
        }
    }

    $("#rationcard input").change(function(){
        toggle_rationcardNum($(this).val()=="Yes")
    })

    toggle_rationcardNum($("#rationcard input:checked").val()=="Yes")
})();  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!