简体   繁体   English

复选框处于隐藏状态,除非选择了下拉值

[英]Check boxes hidden unless a drop down value is selected

I have a form in a CMS system that I am developing. 我正在开发的CMS系统中有一个表单。 In this form I have the code: 这种形式的我有代码:

<div class="styled_select">
    <%= f.select :cat_type, [["Eat & Drink", "eat"], 
    ["Hotels & Bed & Breakfast", "hotel"], 
    ["Attractions & Museums", "attraction"], 
    ["Shopping", "shopping"], ["Art & Design", "art"], 
    ["Health & Beauty", "health"], ["Fix & Repair", "fix"], 
    ["Medical & Safety", "medical"]], {:id => "cat_selector"} %>
</div>

<div class="hidden_option">
why</div>

which constructs a drop down menu. 构造一个下拉菜单。 What I want to do is when I select Shopping, the word why appear. 我要做的是选择“购物”时出现“为什么”一词。 I cannot however seem to make it work. 但是,我似乎无法使其工作。 I have seen some examples but I don;t know what I am not doing wrong. 我已经看到了一些示例,但是我不知道我没有做错什么。 I understand I have to use Javascript but I don't know what and where to put it. 我知道我必须使用Javascript,但我不知道该在哪里放置它。 As you understand I am new in Rails and I could use the help. 如您所知,我是Rails的新手,可以使用帮助。

The Javascript code I use is placed in places.js inside the assets\\javascripts folder 我使用的Javascript代码放置在assets \\ javascripts文件夹内的places.js中

function your_new_method(){
    $("#cat_selector").change(function(){
        if($("#cat_selector").val() == "Shopping"){
            $(".hidden_option").fadeIn('fast');   
        }  
     };
 }

I also have the above css 我也有上面的css

.hidden_option {
display: none;
}

In the application.js file I have put 在application.js文件中,我已经放入

$(document).ready(function(){
   your_new_method(); //Calls the method you created to set up the unobtrusive js
});

You seem to be missing the id defined on the select box. 您似乎缺少选择框上定义的ID。

Place the javascript inside a function and into a js file and store it in the same directory that you find the application.js file. 将javascript放在函数内并放入js文件,并将其存储在与找到application.js文件相同的目录中。 Similar to the following: 类似于以下内容:

function your_new_method(){
    $("#cat_selector").change(function(){
        if($("#cat_selector").val() == "Shopping"){
            $(".hidden_option").fadeIn('fast');   
        }  
     };
 }

Inside application.js there should be a method like the following. 在application.js内部,应该有一个类似于以下的方法。 If it doesnt exist you can just add it. 如果不存在,则可以添加它。

$(document).ready(function(){
   your_new_method(); //Calls the method you created to set up the unobtrusive js
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM