我正在尝试在我的Rails4应用程序中创建一个表单,它有两个选择标记。一个用于类别,另一个用于子类别。我希望子类别中的选项由类别的选择决定。
目前我有这样的表格:
<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>我还有一个助手:
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 )。
我在上面所做的事情有些不对劲,我不知道是什么。
有人能看到我哪里出错了吗?
发布于 2016-06-22 16:41:37
向选择字段添加类:
<%= 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代码。
$(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]);
}
});
});我还没有测试代码。
https://stackoverflow.com/questions/37912691
复制相似问题