Javascript 如何在Rails中预填充动态添加的表单输入
我有一个用于Javascript 如何在Rails中预填充动态添加的表单输入,javascript,jquery,html,ruby-on-rails,forms,Javascript,Jquery,Html,Ruby On Rails,Forms,我有一个用于@profile实例的edit.html.erb表单。通常,当我加载表单时,表单中的输入会预先填充实例的相应属性 e、 g.将从@profile.name预先填充其值 我想使用jQuery在运行时添加额外的字段,允许用户使用额外的动态添加的输入来输入额外的数据 i、 e.@profile.subjects是包含主题及其描述的散列(例如,它可以是{“数学”=>“我教微积分”,“科学”=>“我有物理学位”}) 我从@profile.subjects中检索主题列表,并使用jQuery中的a
@profile
实例的edit.html.erb
表单。通常,当我加载表单时,表单中的输入会预先填充实例的相应属性
e、 g.
将从@profile.name
预先填充其值
我想使用jQuery在运行时添加额外的字段,允许用户使用额外的动态添加的输入来输入额外的数据
i、 e.@profile.subjects
是包含主题及其描述的散列(例如,它可以是{“数学”=>“我教微积分”,“科学”=>“我有物理学位”}
)
我从@profile.subjects
中检索主题列表,并使用jQuery中的append()
将textarea
元素插入每个主题的表单中
因此,如果@profile.subjects
包含“数学”和“科学”,我将在jQuery中执行以下操作:
var subject = *string containing subject*;
parent.append("<textarea id='profile_" + subject + "_desc' name='profile[" + subject + "_desc]'></textarea");
因此,我的解决方法是使用
gon
gem将Rails哈希发送到Javascript,然后根据subject\u names数组调用subject描述
将其添加到控制器(请参见编辑1代码)
然后在Javascript中以以下内容结束:
var subjectNamesInput = $('#tutor_profile_subject_names');
// Initialize subject description input boxes
$.each(subjectNamesInput.tagsinput('items'), function(i, subject){
updateSubjectInputs(subject, 'add');
});
function updateSubjectInputs(subject, action){
var parent = $('#subject-description-inputs');
var subject = escape(subject);
var text = "";
if (gon.subjects[subject] != undefined)
text = gon.subjects[subject];
if (action == 'add'){
parent.append("<textarea id='tutor_profile_" + subject.toLowerCase() + "_description' name='tutor_profile[" +
subject.toLowerCase() + "_description]' class='form-control form-text-area' rows='5' placeholder='Add some detail about your experience with " + subject +
" (optional)'>" + text + "</textarea>");
}
else if (action == 'remove'){
$('#tutor_profile_' + subject.toLowerCase() + '_description').remove();
}
}
var subjectNamesInput=$('tutor#u profile_subject_names');
//初始化主题描述输入框
$.each(subjectNamesInput.tagsinput('items'),函数(i,subject){
更新主体输入(主题“添加”);
});
函数更新SubjectInputs(主题、操作){
var parent=$(“#主题描述输入”);
var主体=逃逸(主体);
var text=“”;
if(gon.subjects[subject]!=未定义)
text=gon.主题[主题];
如果(操作=='add'){
parent.append(“+text+”);
}
else if(操作=='remove'){
$('#tutor_profile.'+subject.toLowerCase()+'_description').remove();
}
}
我认为:
<%= f.text_field :subject_names, class: 'form-control tagsinput typeahead', placeholder: 'Subjects you teach' %>
<div id="subject-description-inputs">
</div>
所以我的解决方法是使用
gon
gem将Rails哈希发送到Javascript,然后根据subject\u names数组调用subject描述
将其添加到控制器(请参见编辑1代码)
然后在Javascript中以以下内容结束:
var subjectNamesInput = $('#tutor_profile_subject_names');
// Initialize subject description input boxes
$.each(subjectNamesInput.tagsinput('items'), function(i, subject){
updateSubjectInputs(subject, 'add');
});
function updateSubjectInputs(subject, action){
var parent = $('#subject-description-inputs');
var subject = escape(subject);
var text = "";
if (gon.subjects[subject] != undefined)
text = gon.subjects[subject];
if (action == 'add'){
parent.append("<textarea id='tutor_profile_" + subject.toLowerCase() + "_description' name='tutor_profile[" +
subject.toLowerCase() + "_description]' class='form-control form-text-area' rows='5' placeholder='Add some detail about your experience with " + subject +
" (optional)'>" + text + "</textarea>");
}
else if (action == 'remove'){
$('#tutor_profile_' + subject.toLowerCase() + '_description').remove();
}
}
var subjectNamesInput=$('tutor#u profile_subject_names');
//初始化主题描述输入框
$.each(subjectNamesInput.tagsinput('items'),函数(i,subject){
更新主体输入(主题“添加”);
});
函数更新SubjectInputs(主题、操作){
var parent=$(“#主题描述输入”);
var主体=逃逸(主体);
var text=“”;
if(gon.subjects[subject]!=未定义)
text=gon.主题[主题];
如果(操作=='add'){
parent.append(“+text+”);
}
else if(操作=='remove'){
$('#tutor_profile.'+subject.toLowerCase()+'_description').remove();
}
}
我认为:
<%= f.text_field :subject_names, class: 'form-control tagsinput typeahead', placeholder: 'Subjects you teach' %>
<div id="subject-description-inputs">
</div>
您能为控制器添加代码吗?这让我头疼。我在最新的编辑中添加了如何在控制器中分配属性的代码。你能添加全部内容吗?代码很容易阅读。人们试图告诉你哪些代码不太重要。我在“编辑1”中对代码进行了更改,以包含实际的编辑操作。你能为控制器添加代码吗?这让我头疼。我在最新的编辑中添加了如何在控制器中分配属性的代码。你能添加全部内容吗?代码很容易阅读。人们试图告诉你什么代码没有那么多。我在编辑1中对代码进行了更改,以包含实际的编辑操作。