0

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()更改有效,但是从选择框中选择另一个选项后,我收到了lengthjquery 错误。

准确地说:

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请求正在停止。有任何想法吗?谢谢。

4

1 回答 1

0

您收到此 jQuery 是因为您没有向obj您的change()函数发送一个。尝试将选定的选项值发送到您在 AJAX 中的操作:

# workout.js.coffee
$(".team-selection-workouts").change ->
    $.post "/workouts/filter_by_team",
    team_id: $(".team-selection-workouts :selected").val() # notice the `:selected` pseudo-selector
于 2013-09-27T15:49:44.820 回答