小编典典

如何修复AJAX复选框时始终触发?

ajax

有时候,这种AJAX会触发,有时却不会,我来解释一下。

habit.js

$(document).ready(function()
{
  $(".habit-check").change(function()
  {
    habit = $(this).parent().siblings(".habit-id").first().attr("id");
    level = $(this).siblings(".level-id").first().attr("id");
    if($(this).is(":checked"))
    {
       $.ajax(
       {
         url: "/habits/" + habit + "/levels/" + level + "/days_missed",
         method: "POST"
       });
    }
    else
    {
       $.ajax(
       {
         url: "/habits/" + habit + "/levels/" + level + "/days_missed/1",
         method: "DELETE"
       });
    }
  });
});

仅在加载显示页面并刷新它之后才会触发,以便终端看起来像这样:

Started GET "/habits/1" for ::1 at 2015-06-20 23:55:42 -0400
Processing by HabitsController#show as HTML
  Parameters: {"id"=>"1"}
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Habit Load (0.1ms)  SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('ingrain'))
  CACHE (0.0ms)  SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  Level Load (0.2ms)  SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = ?  [["habit_id", 1]]
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT  DISTINCT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."tagger_id" = ? AND "taggings"."tagger_type" = ?  ORDER BY taggings_count desc LIMIT 20  [["tagger_id", 1], ["tagger_type", "User"]]
   (0.1ms)  SELECT COUNT(*) FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  Habit Load (0.1ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."id" = ? LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."id" = ? LIMIT 1  [["id", "1"]]
  Habit Load (0.1ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."user_id" = ? AND "habits"."id" = ? LIMIT 1  [["user_id", 1], ["id", 1]]
  CACHE (0.0ms)  SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = ?  [["habit_id", 1]]
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags') GROUP BY tags.id  [["taggable_id", 1], ["taggable_type", "Habit"]]
  Note Load (0.1ms)  SELECT "notes".* FROM "notes" WHERE "notes"."habit_id" = ?  ORDER BY notes_date  [["habit_id", 1]]
  Rendered notes/_notes.html.erb (2.0ms)
  Rendered notes/_form.html.erb (3.9ms)
  User Load (0.1ms)  SELECT "users".* FROM "users" INNER JOIN "habit_likes" ON "users"."id" = "habit_likes"."user_id" WHERE "habit_likes"."habit_id" = ?  [["habit_id", 1]]
  Comment Load (0.1ms)  SELECT "comments".* FROM "comments" WHERE "comments"."habit_id" = ?  [["habit_id", 1]]
  Rendered comments/_comments.html.erb (0.8ms)
  Rendered comments/_form.html.erb (1.0ms)
  Rendered habits/show.html.erb within layouts/application (24.0ms)
   (0.2ms)  SELECT COUNT(*) FROM "notifications" WHERE "notifications"."user_id" = ?  [["user_id", 1]]
  Rendered layouts/_header.html.erb (2.9ms)
   (0.1ms)  SELECT COUNT(*) FROM "habits" WHERE "habits"."user_id" = ? AND 1=0  [["user_id", 1]]
  Rendered habits/_today.html.erb (1.0ms)
  Goal Load (0.2ms)  SELECT  "goals".* FROM "goals" WHERE "goals"."user_id" = ? AND "goals"."accomplished" = 'f'  ORDER BY deadline LIMIT 3  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."followed_id" WHERE "relationships"."follower_id" = ?  [["follower_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "goals" WHERE "goals"."user_id" = ? AND "goals"."accomplished" = 'f'  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "quantifieds" WHERE "quantifieds"."user_id" = ?  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = ?  [["followed_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "goals" WHERE "goals"."user_id" = ? AND "goals"."accomplished" = 't'  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "results" INNER JOIN "quantifieds" ON "results"."quantified_id" = "quantifieds"."id" WHERE "quantifieds"."user_id" = ? AND "results"."bad" = 'f'  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "valuations" WHERE "valuations"."user_id" = ?  [["user_id", 1]]
  Rendered layouts/_count.html.erb (10.7ms)
  Rendered layouts/_tags.html.erb (0.1ms)
  Rendered layouts/_recommendations.html.erb (0.0ms)
  Quantified Load (0.1ms)  SELECT "quantifieds".* FROM "quantifieds" WHERE "quantifieds"."user_id" = ? AND "quantifieds"."categories" = ?  [["user_id", 1], ["categories", "Averaged"]]
  Quantified Load (0.1ms)  SELECT "quantifieds".* FROM "quantifieds" WHERE "quantifieds"."user_id" = ? AND "quantifieds"."categories" = ?  [["user_id", 1], ["categories", "Instance"]]
   (0.1ms)  SELECT COUNT(*) FROM "valuations" WHERE "valuations"."user_id" = ? AND (1 = 0)  [["user_id", 1]]
  Rendered layouts/_value.html.erb (0.8ms)
  Rendered layouts/_sidebar.html.erb (18.9ms)
Completed 200 OK in 324ms (Views: 307.4ms | ActiveRecord: 3.3ms)


Started GET "/assets/application-2b528cffd402884831208f505b651927.css?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/jquery-ea2706bc2d0586731b7c00ec2cd0a414.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/stylesheet-3e834b3a950daa6165191fa42b13a968.css?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/jquery_ujs-29277b946c1d3754af13672bfec0c660.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/bootstrap-d16805677947f8c7ce91cea7ae7f82b0.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/cocoon-07a2b3765fb91baf2df1cea98d97eed0.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/turbolinks-3a7cdb8630f3e86770867bd488a098e0.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/account_activations-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/comments-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/days_missed-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/facebook-faaf7b74474e5a746a0c4a160e4705d2.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/habit-ff66f57b87d00b4469848f1af3f6910a.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/habits-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/activities-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/pages-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/password_resets-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/notes-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/quantified-5cae96afee8ab50e506c00828f3042b3.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/goals-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/quantifieds-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/relationships-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/sessions-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/notifications-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/tags-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/users-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/values-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/application-6fd7d78d93af9f6901c08c3bc66303d0.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/bootstrap/glyphicons-halflings-regular-195cb410b49d75c104a5bc6ad385ac77.woff" for ::1 at 2015-06-20 23:55:43 -0400

仅加载显示页面一次将无法完成工作,因为选中标记不会触发AJAX(请注意上面的所有GET和下面的GET缺少,我认为这与涡轮链接有关)。

Started GET "/habits/1" for ::1 at 2015-06-21 00:09:02 -0400
Processing by HabitsController#show as HTML
  Parameters: {"id"=>"1"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Habit Load (0.1ms)  SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('ingrain'))
  CACHE (0.0ms)  SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  Level Load (0.2ms)  SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = ?  [["habit_id", 1]]
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT  DISTINCT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."tagger_id" = ? AND "taggings"."tagger_type" = ?  ORDER BY taggings_count desc LIMIT 20  [["tagger_id", 1], ["tagger_type", "User"]]
   (0.1ms)  SELECT COUNT(*) FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  Habit Load (0.1ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."id" = ? LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."id" = ? LIMIT 1  [["id", "1"]]
  Habit Load (0.1ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."user_id" = ? AND "habits"."id" = ? LIMIT 1  [["user_id", 1], ["id", 1]]
  CACHE (0.0ms)  SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = ?  [["habit_id", 1]]
  ActsAsTaggableOn::Tag Load (0.1ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags') GROUP BY tags.id  [["taggable_id", 1], ["taggable_type", "Habit"]]
  Note Load (0.1ms)  SELECT "notes".* FROM "notes" WHERE "notes"."habit_id" = ?  ORDER BY notes_date  [["habit_id", 1]]
  Rendered notes/_notes.html.erb (1.1ms)
  Rendered notes/_form.html.erb (3.2ms)
  User Load (0.1ms)  SELECT "users".* FROM "users" INNER JOIN "habit_likes" ON "users"."id" = "habit_likes"."user_id" WHERE "habit_likes"."habit_id" = ?  [["habit_id", 1]]
  Comment Load (0.1ms)  SELECT "comments".* FROM "comments" WHERE "comments"."habit_id" = ?  [["habit_id", 1]]
  Rendered comments/_comments.html.erb (0.9ms)
  Rendered comments/_form.html.erb (1.1ms)
  Rendered habits/show.html.erb within layouts/application (21.4ms)
   (0.1ms)  SELECT COUNT(*) FROM "notifications" WHERE "notifications"."user_id" = ?  [["user_id", 1]]
  Rendered layouts/_header.html.erb (2.3ms)
   (0.1ms)  SELECT COUNT(*) FROM "habits" WHERE "habits"."user_id" = ? AND 1=0  [["user_id", 1]]
  Rendered habits/_today.html.erb (0.9ms)
  Goal Load (0.2ms)  SELECT  "goals".* FROM "goals" WHERE "goals"."user_id" = ? AND "goals"."accomplished" = 'f'  ORDER BY deadline LIMIT 3  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."followed_id" WHERE "relationships"."follower_id" = ?  [["follower_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "goals" WHERE "goals"."user_id" = ? AND "goals"."accomplished" = 'f'  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "quantifieds" WHERE "quantifieds"."user_id" = ?  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = ?  [["followed_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "goals" WHERE "goals"."user_id" = ? AND "goals"."accomplished" = 't'  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "results" INNER JOIN "quantifieds" ON "results"."quantified_id" = "quantifieds"."id" WHERE "quantifieds"."user_id" = ? AND "results"."bad" = 'f'  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "valuations" WHERE "valuations"."user_id" = ?  [["user_id", 1]]
  Rendered layouts/_count.html.erb (9.3ms)
  Rendered layouts/_tags.html.erb (0.0ms)
  Rendered layouts/_recommendations.html.erb (0.1ms)
  Quantified Load (0.1ms)  SELECT "quantifieds".* FROM "quantifieds" WHERE "quantifieds"."user_id" = ? AND "quantifieds"."categories" = ?  [["user_id", 1], ["categories", "Averaged"]]
  Quantified Load (0.1ms)  SELECT "quantifieds".* FROM "quantifieds" WHERE "quantifieds"."user_id" = ? AND "quantifieds"."categories" = ?  [["user_id", 1], ["categories", "Instance"]]
   (0.1ms)  SELECT COUNT(*) FROM "valuations" WHERE "valuations"."user_id" = ? AND (1 = 0)  [["user_id", 1]]
  Rendered layouts/_value.html.erb (0.8ms)
  Rendered layouts/_sidebar.html.erb (17.6ms)
Completed 200 OK in 318ms (Views: 301.9ms | ActiveRecord: 3.1ms)

显示页面 称为AJAX

<div class="strikes">
  <% if @habit.current_level_strike %> 
    <div class="btn" id="red"> <label id="<%= @habit.id %>" class="habit-id">Strikes:</label>
  <% else %> 
    <div class="btn" id="gold"> <label id="<%= @habit.id %>" class="habit-id-two">Strikes:</label>
  <% end %>
    <% @habit.levels.each_with_index do |level, index| %>
      <% if @habit.current_level >= (index + 1) %>
        <p>
          <% if @habit.current_level_strike %> 
            <label id="<%= level.id %>" class="level-id">Level <%= index + 1 %>:</label> 
          <% else %> 
            <label id="<%= level.id %>" class="level-id-two">Level <%= index + 1 %>:</label> 
          <% end %>
          <%= check_box_tag nil, true, level.missed_days > 0, {class: "habit-check"} %>
          <%= check_box_tag nil, true, level.missed_days > 1, {class: "habit-check"} %>
          <%= check_box_tag nil, true, level.missed_days > 2, {class: "habit-check"} %>
       </p>
      <% end %>
    <% end %>
  </div>
</div>

这就是AJAX触发的 源代码days_missed_controller.rb

class DaysMissedController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy]

  def create
    habit = Habit.find(params[:habit_id])
    habit.missed_days = habit.missed_days + 1
    @habit.save!
    level = habit.levels.find(params[:level_id])
    level.missed_days = level.missed_days + 1
    if level.missed_days == 3
      level.missed_days = 0
      level.days_lost += habit.calculate_days_lost + 1
    end
    level.save!
    head :ok # this returns an empty response with a 200 success status code
  end

  def destroy
    habit = Habit.find(params[:habit_id])
    habit.missed_days = habit.missed_days - 1
    habit.save!
    level = habit.levels.find(params[:level_id])
    level.missed_days = level.missed_days - 1
    level.save!
    head :ok # this returns an empty response with a 200 success status code
  end
end

这是 要点
。抱歉,我是AJAX的新手,并获得了帮助,因此我的描述水平可能很基本。请不要犹豫,要求进一步的澄清或代码:-]


更新

因此,换句话说,如果用户加载了显示页面,然后选中了复选框,则没有任何内容发送到服务器;如果用户加载显示页面然后刷新它,然后选中服务器中进行此POST的复选框:

Started POST "/habits/1/levels/1/days_missed" for 127.0.0.1 at 2015-06-22 02:15:21 -0400
Processing by DaysMissedController#create as */*
  Parameters: {"habit_id"=>"1", "level_id"=>"1"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Habit Load (0.2ms)  SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('ingrain'))
  CACHE (0.0ms)  SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  Level Load (0.2ms)  SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = ?  [["habit_id", 1]]
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT  DISTINCT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."tagger_id" = ? AND "taggings"."tagger_type" = ?  ORDER BY taggings_count desc LIMIT 20  [["tagger_id", 1], ["tagger_type", "User"]]
   (0.1ms)  SELECT COUNT(*) FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  Habit Load (0.1ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."user_id" = ? AND "habits"."id" = ? LIMIT 1  [["user_id", 1], ["id", 1]]
  Habit Load (0.1ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."id" = ? LIMIT 1  [["id", 1]]
   (0.1ms)  begin transaction
  CACHE (0.0ms)  SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = ?  [["habit_id", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)  [["taggable_id", 1], ["taggable_type", "Habit"]]
  CACHE (0.0ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)  [["taggable_id", 1], ["taggable_type", "Habit"]]
  ActsAsTaggableOn::Tag Load (0.1ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags' AND
                                    taggings.tagger_id = 1 AND
                                    taggings.tagger_type = 'User')  [["taggable_id", 1], ["taggable_type", "Habit"]]
   (0.1ms)  commit transaction
  Level Load (0.2ms)  SELECT  "levels".* FROM "levels" WHERE "levels"."habit_id" = ? AND "levels"."id" = ? LIMIT 1  [["habit_id", 1], ["id", 1]]
   (0.1ms)  begin transaction
  SQL (0.3ms)  UPDATE "levels" SET "missed_days" = ?, "updated_at" = ? WHERE "levels"."id" = ?  [["missed_days", 1], ["updated_at", "2015-06-22 06:15:21.959609"], ["id", 1]]
   (2.8ms)  commit transaction
Completed 200 OK in 40ms (ActiveRecord: 5.4ms)

阅读 185

收藏
2020-07-26

共1个答案

小编典典

这可能是Turbolinks的问题,您是否可以尝试更改javascript:

$(document).ready(function()
{
  // ...
}

看起来像:

$(document).on("ready page:load", function() {
  // ..
}

请让我知道是否有帮助!

祝好运!

2020-07-26