使用f.select选择课程,并自动输入count_stud值字段
Chose a course using f.select and automatically entered into the field count_stud value
我们可以通过表格选择年份和课程。另一个字段不可见(它应该改变与所选课程相对应的学生人数)。С使用f.select选择一门课程,并自动输入字段count_stud值,该值等于该课程的学生人数。如何做到这一点?可能需要JavaScript(Ajax技术)
使用ajax
可以做到这一点,更改您的选择,如下面的
<%= f.select(:course_id, @courses.map{|p| [p.id]},{},{:class => "course_id"}) %>
年份:
<%= f.select(:year_id, @years.map{|p| [p.name, p.id]} ,{},{:onchange => "update_student(this.value)"}) %>
文本框:
<%= f.text_field :count_stud, :value => @student_count,:class => "student" %>
在ajax代码中:
function update_student(year) {
var course = jQuery(".course_id").val(); // finding course value
if(course == ""){ // checking course value being selected or not
alert("Please select course ");
return false;
}
jQuery.ajax({
url: "update_student",
type: "GET",
data: {"course_id" : course,"year": year },
dataType: "text",
success: function(data) {
jQuery(".student").val(data); // inserting response to text field.
}
});
}
因此,课程选择框的onchange
一个ajax请求将带有年份和课程值,然后在您的操作中获得params
中的值,找到no of student
并返回如下在YearCoursesController
中
def update_student
year = params[:year]
course = params[:course]
#write logic to find student
#assign in one variable
year_courses = YearCourse.find_by(year_id: year, course_id: course)
student_count = year_courses ? year_courses.students.count : 0
render :text => student_count #we are returning the value
end
则该值将插入到文本框中。
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 可以't让我的if语句处理js中的html表单输入
- 名称输入的索引
- 如何编写HTML输入的JS内联
- 要求输入在数据列表中
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 让文本输入幻灯片显示输入时的新文本输入?然后向后滑动
- 如何将输入(type=text)从html表单传递到javascript函数
- 单击jquery清除输入值
- 而循环只设置php中输入字段中的第一个值
- 在输入字段中将最小金额设置为
- jQuery自定义验证比较多个输入的序列
- Sails.js:同时发布文本输入和一个文件
- 使用javascript检查多个输入值,并在1次检查中标记多个输入框
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- 使用jquery在单击时在单元格中输入值
- Ajax文件加载和<输入>文件加载
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 将值输入到对象,然后该对象推送到数组
- 如果我在javascript中输入无效的电子邮件或空白,如何显示特定的文本框边框红色