<bdo id='U2V0o'></bdo><ul id='U2V0o'></ul>
  • <small id='U2V0o'></small><noframes id='U2V0o'>

  • <tfoot id='U2V0o'></tfoot>

      <i id='U2V0o'><tr id='U2V0o'><dt id='U2V0o'><q id='U2V0o'><span id='U2V0o'><b id='U2V0o'><form id='U2V0o'><ins id='U2V0o'></ins><ul id='U2V0o'></ul><sub id='U2V0o'></sub></form><legend id='U2V0o'></legend><bdo id='U2V0o'><pre id='U2V0o'><center id='U2V0o'></center></pre></bdo></b><th id='U2V0o'></th></span></q></dt></tr></i><div id='U2V0o'><tfoot id='U2V0o'></tfoot><dl id='U2V0o'><fieldset id='U2V0o'></fieldset></dl></div>
      <legend id='U2V0o'><style id='U2V0o'><dir id='U2V0o'><q id='U2V0o'></q></dir></style></legend>

      1. 使用复选框、AJAX、jQuery 更改属性

        Change attribute using checkbox, AJAX, jQuery(使用复选框、AJAX、jQuery 更改属性)
        <i id='VFO84'><tr id='VFO84'><dt id='VFO84'><q id='VFO84'><span id='VFO84'><b id='VFO84'><form id='VFO84'><ins id='VFO84'></ins><ul id='VFO84'></ul><sub id='VFO84'></sub></form><legend id='VFO84'></legend><bdo id='VFO84'><pre id='VFO84'><center id='VFO84'></center></pre></bdo></b><th id='VFO84'></th></span></q></dt></tr></i><div id='VFO84'><tfoot id='VFO84'></tfoot><dl id='VFO84'><fieldset id='VFO84'></fieldset></dl></div>

        <small id='VFO84'></small><noframes id='VFO84'>

          <tbody id='VFO84'></tbody>
          <bdo id='VFO84'></bdo><ul id='VFO84'></ul>
          <legend id='VFO84'><style id='VFO84'><dir id='VFO84'><q id='VFO84'></q></dir></style></legend>

                <tfoot id='VFO84'></tfoot>

                  本文介绍了使用复选框、AJAX、jQuery 更改属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我希望根据是否选中复选框来更改我的任务资源的布尔属性.我被卡住了,因为我不知道该怎么做......在添加这个 AJAX 复选框之前我已经拥有了一切,整个 CRUD,一切都用 rspec 和 capybara 测试并引导.我有以下代码...

                  I wish to change the boolean attribute of my Task resource depending on wether checkbox is checked or not. I am stuck as I don't know what to do... I have everything before adding this AJAX checkbox working, whole CRUD, everything tested with rspec and capybara and bootstrapped. I have the following code...

                  views/tasks/show.html.erb

                   27      <%= form_for [@project, @task], :remote => true do |f| %>
                   28        <%= f.label :completed %>       
                   29        <%= f.check_box :completed %>
                   30      <% end %>
                  

                  这个 :completed 是任务的布尔属性.

                  This :completed is the boolean attribute of the Task.

                  javascripts/tasks.js.coffee

                    8 jQuery ->
                    9   $('#task_completed').bind 'change', (event) =>
                   10     $.post()
                  

                  不知道如何完成这个 $.post() 事情以及如何使控制器中的代码工作......到目前为止我只有这个......

                  Dont know how to finish this $.post() thing and how to make code in the controller work... So far I only have this...

                  controllers/tasks_controller.rb

                   1 class TasksController < ApplicationController
                    2 
                    3   before_filter :authenticate_user!
                    4   before_filter :find_project_from_project_id
                    5   before_filter :find_task, :only => [:show, :edit, :update]
                    6 
                    7   def show
                    8     @title = @task.name
                    9   end
                   10 
                   11   def new
                   12     @title = 'New task'
                   13     @task = @project.tasks.build
                   14   end
                   15 
                   16   def create
                   17     @task = @project.tasks.build(params[:task])
                   18     if @task.save
                   19       current_user.tasks.push(@task)
                   20       redirect_to [@project, @task], :notice => 'Task created'
                   21     else
                   22       render 'new'
                   23       flash.now[:alert] = 'Task not created'
                   24     end
                   25   end 
                   26     
                   27   def edit 
                   28     @title = 'Edit task'
                   29   end 
                   30   
                   31   def update
                   32     if @task.update_attributes(params[:task])
                   33       redirect_to [@project, @task], :notice => 'Task updated'
                   34     else
                   35       render 'edit'
                   36       flash.now[:alert] = 'Task not updated'
                   37     end
                   38   end 
                   39 
                   40   private
                   41   #controller doesn't respond to these methods as actions
                   42   
                   43   def find_project_from_project_id
                   44     @project = current_user.projects.find(params[:project_id])
                   45     rescue ActiveRecord::RecordNotFound
                   46       redirect_to projects_path
                   47       flash[:alert] = 'Project you were looking for could not be found'
                   48   end
                   49 
                   50   def find_task
                   51     @task = @project.tasks.find(params[:id])
                   52     rescue ActiveRecord::RecordNotFound
                   53       redirect_to project_path(@project)
                   54       flash[:alert] = 'Task you were looking for could not be found'
                   55   end
                   56 
                   57 end
                  

                  另外,对于那些想要更多的人......如何为这个东西编写测试?我应该为此写它们吗?

                  经过研究,我发现人们正在这样做......这是要走的路吗?在检查元素时,我可以看到当我更改复选框时正在发出新的请求,但布尔值仍然是错误的......

                  Upon research, I found people are doing it like this... Is this the way to go? Upon inspecting element I can see new requests are being made when i change the checkbox, but the boolean still remains false...

                    8 jQuery ->
                    9   $('#task_completed').bind 'change', (event) =>
                   10     $('#task_completed').parents('form:first').submit()
                   11     $('.task_headline').toggleClass('completed_task')
                  

                  推荐答案

                  这在很多方面都不是一个优雅的解决方案,但它确实有效:)

                  This is not an elegant solution in so many ways, but it works :)

                  show.html.erb

                  <script>
                  window.monitorTaskCompletion(1,1)
                  </script>
                  

                  tasks.coffee

                  window.monitorTaskCompletion = (project_id, task_id)  ->
                      jQuery ->
                        $('#task_completed').bind 'change', (event) =>
                          $.ajax
                            url: "/projects/#{project_id}/tasks/#{task_id}/complete"
                            data: {completed: if $('#task_completed').is(':checked') then 1 else 0}
                            type: "PUT"
                  

                  tasks_controller.rb

                  def complete
                    @completed = params[:completed].to_i > 0
                    @task.complete(@completed)
                    respond_to :js
                  end
                  

                  这篇关于使用复选框、AJAX、jQuery 更改属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                  ExecJS::ProgramError: SyntaxError: Reserved word quot;functionquot;(ExecJS::ProgramError: SyntaxError: 保留字“function)
                  Infinite scroll and will_paginate appending the #39;next page#39; of items multiple times(无限滚动和 will_paginate 多次附加项目的“下一页)
                  Set of CoffeeScript/JavaScript classes and methods available to rest of Rails app(一组可供 Rails 应用程序使用的 CoffeeScript/JavaScript 类和方法)
                    <tbody id='oOZfO'></tbody>

                    <bdo id='oOZfO'></bdo><ul id='oOZfO'></ul>
                  • <legend id='oOZfO'><style id='oOZfO'><dir id='oOZfO'><q id='oOZfO'></q></dir></style></legend>

                    <small id='oOZfO'></small><noframes id='oOZfO'>

                  • <i id='oOZfO'><tr id='oOZfO'><dt id='oOZfO'><q id='oOZfO'><span id='oOZfO'><b id='oOZfO'><form id='oOZfO'><ins id='oOZfO'></ins><ul id='oOZfO'></ul><sub id='oOZfO'></sub></form><legend id='oOZfO'></legend><bdo id='oOZfO'><pre id='oOZfO'><center id='oOZfO'></center></pre></bdo></b><th id='oOZfO'></th></span></q></dt></tr></i><div id='oOZfO'><tfoot id='oOZfO'></tfoot><dl id='oOZfO'><fieldset id='oOZfO'></fieldset></dl></div>

                          <tfoot id='oOZfO'></tfoot>