如何存储课程以外的下拉菜单和打开新的文本框放其他教育
How to store course other than dropdown menu and open new textbox put other educations?
模型领域-学位,研究所,百分比这些是模型领域。
如果课程不是下拉菜单,那么选择"其他"并显示输入用户教育的文本框。当表单提交时,我想在学位列中存储教育。但是现在我不能处理这两个值。
education.html。erb文件
<p>
<%= f.label :user_id, "Select User.*" %>
<% if params[:user_id] != nil %>
<%= f.select :user_id, :collection => User.where(:id => params[:user_id]).map{|u| ["#{u.firstname}, #{u.lastname}", u.id]} %>
<% else %>
<% @selected_user_id = EducationInformations.where(:id => params[:id]).pluck(:user_id).first %>
<%= f.select :user_id, :collection => User.where(:id => @selected_user_id).map{|u| ["#{u.firstname}, #{u.lastname}", u.id]} %>
<% end %>
</p>
<br/>
<% course = ["MCA","BCA","BSC","Engg","Diploma in computer science","Other"] %>
<p>
<%= f.label :degree, "Select Course.*" %>
<%= f.select :degree , :collection => (course) %>
</p>
<div id="other_edu">
<br/>
<p>
<%= f.label :other_degree, "Enter your course" %>
<%= f.text_field :other_degree , :id => "other_course", :onblur => "getCourse()" %>
</p>
</div>
<br/>
<p>
<%= f.label :institute, "Institute*" %>
<%= f.text_field :institute %>
</p>
<br/>
<p>
<%= f.label :percentage, "Grade/Percentage" %>
<%= f.text_field :percentage %>
</p>
javascript
$(document).ready(function() {
var select = document.getElementById('education_informations_degree');
var other_course = document.getElementById('other_course');
$('#other_edu').hide();
select.onchange = function() {
var selected_course = select.value;
if (selected_course == "Other")
{
$('#other_edu').show();
}else{
$('#other_edu').hide();
}
}
});
function getCourse() {
var x = document.getElementById("other_course");
var select = document.getElementById('education_informations_degree');
select.value = x.value;
console.log(x.value);
}
Model
class EducationInformations < ActiveRecord::Base
unloadable
attr_accessible :user_id, :degree, :institute, :passing_year, :country, :university, :percentage
validates :degree, presence: true
validates :passing_year, presence: true
validates :university, presence: true
validates :institute, presence: true
end
Controller
def create
@edu_info = EducationInformations.new(educational_params)
if @edu_info.save
#flash[:notice] = 'Vote saved.'
redirect_to @edu_info
else
render "new"
end
end
private
def educational_params
params.require(:education_informations).permit(:user_id, :degree, :institute, :passing_year, :country, :university, :percentage)
end
当用户从下拉列表中选择值时,我如何修复此问题,因此我将值设置为文本框值。如果我选择"其他"项目,那么显示文本框和用户可以输入的任何内容,所以我发送文本值。我知道这不是最好的办法,但我认为这个办法能解决我的问题。
视图代码
<% course = ["MCA","BCA","BSC","Engg","Diploma in computer science","Other"] %>
<p>
<%= label_tag "select_degree", "Select Course *" %>
<%= select_tag "select_degree" , options_for_select(course) %>
</p>
<div id="other_edu">
<br/>
<p>
<%= f.label :degree, "Enter your course *" %>
<%= f.text_field :degree %>
</p>
</div>
<br/>
<p>
<%= f.label :institute, "Institute *" %>
<%= f.text_field :institute %>
</p>
<br/>
<p>
<%= f.label :percentage, "Grade/Percentage " %>
<%= f.text_field :percentage %>
</p>
Javascript
$(document).ready(function() {
var select = document.getElementById('select_degree');
var other_course = document.getElementById('education_informations_degree');
$('#other_edu').hide();
select.onchange = function() {
var selected_course = select.value;
if (selected_course == "Other")
{
$('#other_edu').show();
}else{
$('#other_edu').hide();
other_course.value = select.value;
}
}
});
相关文章:
- JavaScript在其他页面上用html内容填充文本框
- 使用其他页面上表单的文本创建链接
- 分析Adobe InDesign的HTML文本——其他标记中的标记
- 如何将文本从一个文本字段复制到其他文本字段
- 从下拉列表中选择其他选项时激活文本框
- 在其他网站上查找/着色/标记文本
- 当其他事件发生时,用javascript隐藏的文本框将变为可见
- 在其他文本输入字段中输入文本时启用复选框
- 更改包含其他元素的元素的文本
- 在HTML文本框中查找某些子字符串,然后用其他字符串替换它们
- 使用appendchild或其他方法向ul添加html代码而不是纯文本
- 如果我用javascript隐藏了一个文本输入元素,那么其他javascript函数仍然可以访问输入到其中的值
- 如何在jquery中使用其他下拉菜单来设置下拉菜单的文本和值
- 即使在重新加载页面后也选择了其他文本框,则显示隐藏的文本框
- JQuery文本编辑器-在工具栏中添加其他下拉列表
- 清除文本区域会中断其他JS函数
- 如果其他字段包含文本,则阻止文本字段
- 在任一下拉列表中选择“其他”时,两个下拉菜单将打开一个文本框
- 如何将用户输入传递到其他文本区域
- 使用 javascript 或 flash 或其他任何东西从跨域 iframe 复制文本