我length
在 Javascript 中遇到未定义的错误。
更改后我有一个 ajaxpost
函数select tag
index.html.erb:
<%= select_tag :team_id, options_from_collection_for_select(Team.where(campus_id:current_user.campus_id).order('name'), 'id', 'name'), class:"team-selection-workouts", prompt:"Select" %>
更改锻炼.js.coffee 中的功能:
$(".team-selection-workouts").change ->
$.post "/workouts/filter_by_team",
team_id: $(".team-selection-workouts").val()
路线.rb
resources :workouts do
collection do
post :filter_by_team
end
end
控制器动作workouts_controller.rb
所以我team_id
使用上面咖啡脚本中的 ajax 传递值。
def filter_by_team
# For Trainers
@team = Team.find(params[:team_id])
@workouts = Workout.find(:all, order: 'name', :conditions => ['team_id = ? and status = ?', @team.id, 'new'])
end
在我的 filter_by_team.js.erb 中:
我传递@workouts
了部分对象
$('#team-workouts').html("<%=j render :partial => 'workouts/filtered_by_team', locals: {workouts: @workouts}%>");
每个选择框change()
的部分必须根据team_id
_filtered_by_team.html.erb:
现在将本地人传递workouts
给部分文件......我有一个多选框......
<%= select_tag :workout, options_from_collection_for_select(workouts, "id", "name"), size: 7, class: 'excercises_muliti_select' %>
第一次select()
更改有效,但是从选择框中选择另一个选项后,我收到了length
jquery 错误。
准确地说:
Uncaught TypeError: Cannot read property 'length' of undefined jquery.js?body=1:606
jQuery.extend.each jquery.js?body=1:606
(anonymous function) jqBootstrapValidation.js?body=1:459
jQuery.extend.each jquery.js?body=1:632
jQuery.fn.jQuery.each jquery.js?body=1:254
(anonymous function) jqBootstrapValidation.js?body=1:457
jQuery.event.dispatch jquery.js?body=1:3046
elemData.handle
因此,因此post
请求正在停止。有任何想法吗?谢谢。