首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails -依次填充类别和子类别的选择菜单

Rails -依次填充类别和子类别的选择菜单
EN

Stack Overflow用户
提问于 2016-06-20 06:58:45
回答 1查看 159关注 0票数 0

我正在尝试在我的Rails4应用程序中创建一个表单,它有两个选择标记。一个用于类别,另一个用于子类别。我希望子类别中的选项由类别的选择决定。

目前我有这样的表格:

代码语言:javascript
复制
<div class="nested-fields">
<div class="container-fluid">

  <div class="form-inputs">

    <%= f.input :irrelevant, :as => :boolean, :label =>  "Is an ethics review required or applicable to this project?"  %>

    <%= f.input :category, collection: [ "Risk of harm", "Informed consent", "Anonymity and Confidentiality", "Deceptive practices", "Right to withdraw"], :label => "Principle",  prompt: 'select' %>

    <%= f.input :subcategory,  collection: text_for_subcategory(@category), :label => "Subcategory", prompt: 'select'  %>



    </div>
  </div>
  </div>

我还有一个助手:

代码语言:javascript
复制
module EthicsHelper
    def text_for_subcategory(category)
      if category == 'Risk of harm'
            [ "Physical Harm", "Psychological distress or discomfort", "Social disadvantage", "Harm to participants", "Financial status", "Privacy"]
        elsif category == 'Informed consent'
            ["Explanation of research", "Explanation of participant's role in research"]
        elsif category == 'Anonymity and Confidentiality'
            ["Remove identifiers", "Use proxies", "Disclosure for limited purposes"]
        elsif category == 'Deceptive practices' 
            ["Feasibility"] 
        else category == 'Right to withdraw'    
            ["Right to withdraw from participation in the project"] 
       end
    end  

end

当我尝试这样做时,不管我在category字段中做了什么选择,subcategory字段都只填充了最后一个属性(right the )。

我在上面所做的事情有些不对劲,我不知道是什么。

有人能看到我哪里出错了吗?

EN

回答 1

Stack Overflow用户

发布于 2016-06-22 16:41:37

向选择字段添加类:

代码语言:javascript
复制
<%= f.input :category, collection: [ "Risk of harm", "Informed consent", "Anonymity and Confidentiality", "Deceptive practices", "Right to withdraw"], :label => "Principle",  prompt: 'select', class: "main_category" %>

<%= f.input :subcategory,  collection: text_for_subcategory(@category), :label => "Subcategory", prompt: 'select', class: "sub_category" %>

添加以下javascript代码。

代码语言:javascript
复制
$(document).ready(function() {
  $(".main_category").change(function(){
    var category = $(".main_category").val(),
        sub_category = $(".sub_category"),
        options = [],
        str = "";
    sub_category.find('option').remove();

    if(category == 'Risk of harm'){
      options = ["Physical Harm", "Psychological distress or discomfort", "Social disadvantage", "Harm to participants", "Financial status", "Privacy"]
    }
    else if(category == 'Informed consent'){
      options = ["Explanation of research", "Explanation of participant's role in research"]   
    }
    else if(category == 'Anonymity and Confidentiality'){
      options = ["Remove identifiers", "Use proxies", "Disclosure for limited purposes"]
    }
    else if(category == 'Deceptive practices'){
      options = ["Feasibility"]
    }
    else if(category == 'Right to withdraw'){
      options = ["Right to withdraw from participation in the project"]  
    }
    if(options.length > 0){
      for(i=0;i<options.length;i++){
        str = '<option value="' + options[i] + '">' + options[i] + '</option>'
        sub_category.append(str);
      }
      sub_category.val(options[0]);
    }
  });
});

我还没有测试代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37912691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档