d同意 匿名性和保密性 欺骗行为 撤回的权利 我将在本例中使用ajax,仍然使用jquery检测第一个选择列表中的更改,然后通过ajax触发控制器操作以重新加载第二个列表如果第二个字段的数据没有被某种形式的管理面板更改,那么您可以在JS中轻松实现这一点。否
d同意 匿名性和保密性 欺骗行为 撤回的权利d同意 匿名性和保密性 欺骗行为 撤回的权利 我将在本例中使用ajax,仍然使用jquery检测第一个选择列表中的更改,然后通过ajax触发控制器操作以重新加载第二个列表如果第二个字段的数据没有被某种形式的管理面板更改,那么您可以在JS中轻松实现这一点。否,jquery,ruby-on-rails,forms,select,drop-down-menu,Jquery,Ruby On Rails,Forms,Select,Drop Down Menu,d同意 匿名性和保密性 欺骗行为 撤回的权利 我将在本例中使用ajax,仍然使用jquery检测第一个选择列表中的更改,然后通过ajax触发控制器操作以重新加载第二个列表如果第二个字段的数据没有被某种形式的管理面板更改,那么您可以在JS中轻松实现这一点。否则,一旦用户选择了第一个字段,您就需要使用AJAX获取第二个字段的选项。@JagjotSingh-每个类别的子类别都在“我的助手”中定义。@Mel这意味着它们将是非常静态的,不会有太多的更改?没错-它们不太可能更改建议。我不明白如何遵循js代码
我将在本例中使用ajax,仍然使用jquery检测第一个选择列表中的更改,然后通过ajax触发控制器操作以重新加载第二个列表如果第二个字段的数据没有被某种形式的管理面板更改,那么您可以在JS中轻松实现这一点。否则,一旦用户选择了第一个字段,您就需要使用AJAX获取第二个字段的选项。@JagjotSingh-每个类别的子类别都在“我的助手”中定义。@Mel这意味着它们将是非常静态的,不会有太多的更改?没错-它们不太可能更改建议。我不明白如何遵循js代码中的说明。您有没有关于如何通过值设置类别/解析的示例?或者我在哪里可以找到关于如何做到这一点的信息?另外,当我使用jqeury时,我可以在我的项目中使用它吗?这个ajax调用是绝对多余的,这个if-else语句可以在客户机上解决side@mixan946是的,这太过分了,总有一天他可能想从数据库中查询数据DB@blnc即使他想要从数据库中拾取数据,也没有理由每次更改select时都查询此数据。谢谢。我可以看出您的代码片段在这个示例中工作得非常好。不过,我正在努力将它插入到我的代码中。这可能与简单的表单集合结构有关。我更新了我的帖子以显示代码中的内容。我无法按您在示例中所示的方式替换表单元素。表单呈现时没有任何错误,但第二个菜单为空。@Mel我阅读了您的新示例。这对我来说似乎不正确,
“子类别”,提示:“选择”,id:“子类别”,选项:“已禁用”%>
使用select代替
此处是此输入的完整文档,我尝试了这种方法。它呈现页面,第二个输入字段有一个空的选择菜单。它最初应该是空的,在第一个选择上选择值后,它应该被激活。你的箱子里总是空的吗?
jQuery(document).ready(function() {
jQuery("#project_ethic_attributes.main_category").change(function() {
var category = $("#project_ethic_attributes.main_category").val(),
sub_category = $("#project_ethic_attributes.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"]
}
});
// jQuery(".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]);
// }
});
<%= f.simple_fields_for :ethics do |f| %>
<%= render 'ethics/ethic_fields', f: f %>
<% end %>
<%= link_to_add_association 'Add an ethics consideration', f, :ethics, partial: 'ethics/ethic_fields' %>
<%= f.input :category, collection: [ "Risk of harm", "Informed consent", "Anonymity and Confidentiality", "Deceptive practices", "Right to withdraw"], :label => "Principle", prompt: 'select', id: "main_category" %>
<%= f.input :subcategory, collection: text_for_subcategory(@category), :label => "Subcategory", prompt: 'select', id: "sub_category" %>
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
<%= f.input :category, collection: [ "Risk of harm", "Informed consent", "Anonymity and Confidentiality", "Deceptive practices", "Right to withdraw"], option:'disabled selected value', :label => "Principle", prompt: 'select', id: "main_category" %>
<%= f.input :subcategory, :label => "Subcategory", prompt: 'select', id: "sub_category", option:'disabled' %>
jQuery(document).ready(function() {
var optionsMap = {
'Risk of harm': [
'Physical Harm',
'Psychological distress or discomfort',
'Social disadvantage',
'Harm to participants',
'Financial status',
'Privacy'
],
'Informed consent': [
'Explanation of research',
"Explanation of participant's role in research"
],
'Anonymity and Confidentiality': [
'Remove identifiers', 'Use proxies', 'Disclosure for limited purposes'
],
'Deceptive practices': [
'Feasibility'
],
'Right to withdraw': [
'Right to withdraw from participation in the project'
]
};
jQuery('#main_category').change(function() {
var category = jQuery(this).val(),
$subCategory = jQuery('#sub_category'),
newOptions = optionsMap[category];
$subCategory.attr('disabled', false)
$subCategory.empty();
$.each(newOptions, function() {
$subCategory.append(jQuery("<option></option>").text(this));
});
})
});
<%= f.select :category, collection: [ "Risk of harm", "Informed consent", "Anonymity and Confidentiality", "Deceptive practices", "Right to withdraw"], option:'disabled selected value', :label => "Principle", prompt: 'select', html: { id: "main_category" } %>
<%= f.select :subcategory, [], {}, id: "sub_category", disabled: true %>
// Dynamic Search Model Loader
$(function(){
var saved = {};
var options, value;
$("#selectOne").change(function(){
options = '<option value="" selected="selected">Loading...</option>';
$("#selectTwo").html(options);
value = $(this).val();
if (saved[value].length > 0) {
$("#selectTwo").html(saved[value]);
} else {
$.ajax({
url: '/api/get-json-values/?category=' + value, // Set the category as the value or whatever you want it to be
type: 'GET',
dataType: 'json',
async: true
}).done(function(data){
options = '<option value="" selected="selected">Please Pick...</option>';
// Parse through the values
$.each(data, function(text){
options += '<option value="' + text + '">' + text + '</option>';
});
// Populate the second select menu
$("#selectTwo").html(options);
saved[value] = options;
}).fail(function(data){
options = '<option value="" selected="selected">Empty...</option>';
$("#selectTwo").html(options);
}); // End Ajax
};
}); // End Change
});
namespace :api do
get 'get-json-values', to: 'queries#category_search'
end
class API::Queries < ApplicationController
def category_search
category = params[:category]
if category == 'Risk of harm'
@json = [ "Physical Harm", "Psychological distress or discomfort", "Social disadvantage", "Harm to participants", "Financial status", "Privacy"]
elsif category == 'Informed consent'
@json = ["Explanation of research", "Explanation of participant's role in research"]
elsif category == 'Anonymity and Confidentiality'
@json = ["Remove identifiers", "Use proxies", "Disclosure for limited purposes"]
elsif category == 'Deceptive practices'
@json = ["Feasibility"]
elsif category == 'Right to withdraw'
@json = ["Right to withdraw from participation in the project"]
else
@json = nil
end
if @json
render json: @json.to_json, status: 200
else
render json: {message: 'Not Found'}, status: 404
end
end
end
var optionsMap = {
'Risk of harm': [
'Physical Harm',
'Psychological distress or discomfort',
'Social disadvantage',
'Harm to participants',
'Financial status',
'Privacy'
],
'Informed consent': [
'Explanation of research',
"Explanation of participant's role in research"
],
'Anonymity and Confidentiality': [
'Remove identifiers', 'Use proxies', 'Disclosure for limited purposes'
],
'Deceptive practices': [
'Feasibility'
],
'Right to withdraw': [
'Right to withdraw from participation in the project'
]
};
jQuery('#main_category').change(function() {
var category = jQuery(this).val(),
$subCategory = jQuery('#sub_category'),
newOptions = optionsMap[category];
$subCategory.empty();
$.each(newOptions, function(key,value) {
$subCategory.append(jQuery("<option></option>").attr("value", value).text(key));
});
})