Javascript 如何使用dropdownlist选定值刷新表?
我的任务中有一个方法。\u controller.rbJavascript 如何使用dropdownlist选定值刷新表?,javascript,jquery,ruby-on-rails,Javascript,Jquery,Ruby On Rails,我的任务中有一个方法。\u controller.rb def index @tasks = Task.all end 我使用table来显示@tasks 我该怎么做?我搜索这个问题,它似乎像使用onchange和javascript可以做我想做的,有人可以给我一些提示,我是一个完全的javascript新手。。。 谢谢 我将:'data-remote'=>'true',:'data-type'=>'HTML'添加到我的select中,我从服务器上获得了正确的响应我的selec
def index
@tasks = Task.all
end
我使用table来显示@tasks
我该怎么做?我搜索这个问题,它似乎像使用onchange和javascript可以做我想做的,有人可以给我一些提示,我是一个完全的javascript新手。。。
谢谢
我将:'data-remote'=>'true',:'data-type'=>'HTML'添加到我的select中,我从服务器上获得了正确的响应我的selected值以进行搜索,就像我进入页面一样,但页面不会刷新。什么问题
Started GET "/tasks/new" for IP at 2015-08-10 10:24:15 +0800
Processing by TasksController#new as HTML
Account Load (0.3ms) SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`id` = 2 LIMIT 1
Locate Load (0.2ms) SELECT `locates`.* FROM `locates`
Account Load (0.3ms) SELECT `accounts`.* FROM `accounts` WHERE (manager_id = 2)
Beacon Load (0.5ms) SELECT `beacons`.* FROM `beacons` WHERE (account_id = 2) AND (beacon_status < 3)
Category Load (0.3ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1 [["id", 1]]
Category Load (0.2ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1 [["id", 2]]
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1 [["id", 2]]
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1 [["id", 1]]
Rendered tasks/_form.html.erb (9.1ms)
Rendered tasks/new.html.erb within layouts/application (9.9ms)
Rendered common/_navbar.html.erb (0.5ms)
Rendered common/_footer.html.erb (0.1ms)
Completed 200 OK in 64ms (Views: 60.4ms | ActiveRecord: 1.8ms)
Started GET "/tasks/new?task%5Blocate_id%5D=1" for IP at 2015-08-10 10:24:19 +0800
Processing by TasksController#new as HTML
Parameters: {"task"=>{"locate_id"=>"1"}}
Account Load (0.3ms) SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`id` = 2 LIMIT 1
Locate Load (0.2ms) SELECT `locates`.* FROM `locates`
Account Load (0.2ms) SELECT `accounts`.* FROM `accounts` WHERE (manager_id = 2)
Beacon Load (0.3ms) SELECT `beacons`.* FROM `beacons` WHERE (account_id = 2) AND (beacon_status < 3) AND (locate_id = '1')
Category Load (0.2ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1 [["id", 1]]
Category Load (0.2ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1 [["id", 2]]
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1 [["id", 1]]
Rendered tasks/_form.html.erb (7.8ms)
Rendered tasks/new.html.erb within layouts/application (14.5ms)
Rendered common/_navbar.html.erb (0.6ms)
Rendered common/_footer.html.erb (0.0ms)
Completed 200 OK in 85ms (Views: 82.4ms | ActiveRecord: 1.5ms)
您可以使用更改事件。从下拉列表中选择选项后,编写事件处理程序以捕获此事件。请参阅。在handler函数中,向您的服务器发送一条消息。Ajax请求有一个成功处理程序,它基本上是一个在请求成功完成时执行的函数。在此函数中,其中一个参数是服务器发送的数据,并根据需要动态更新表。为此,您可能需要在服务器中使用JSON API。标准的HTML响应将不起作用。谢谢您的建议,但是经过一段时间的阅读,我仍然无法理解这个案例有点紧急,所以我没有太多时间阅读,我添加了一些我尝试过的方法,如果您有时间,请看一看。
<%= f.label 'Task' %>: <%= f.select :locate_id, Locate.all.collect { |l| [ l.locate_name, l.id ] }, {:prompt => 'choose'}, :onchange => '$.get(#{/index})' %>
@tasks = Task.where('locate_id = ?', params[:task][:locate_id])
Started GET "/tasks/new" for IP at 2015-08-10 10:24:15 +0800
Processing by TasksController#new as HTML
Account Load (0.3ms) SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`id` = 2 LIMIT 1
Locate Load (0.2ms) SELECT `locates`.* FROM `locates`
Account Load (0.3ms) SELECT `accounts`.* FROM `accounts` WHERE (manager_id = 2)
Beacon Load (0.5ms) SELECT `beacons`.* FROM `beacons` WHERE (account_id = 2) AND (beacon_status < 3)
Category Load (0.3ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1 [["id", 1]]
Category Load (0.2ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1 [["id", 2]]
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1 [["id", 2]]
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1 [["id", 1]]
Rendered tasks/_form.html.erb (9.1ms)
Rendered tasks/new.html.erb within layouts/application (9.9ms)
Rendered common/_navbar.html.erb (0.5ms)
Rendered common/_footer.html.erb (0.1ms)
Completed 200 OK in 64ms (Views: 60.4ms | ActiveRecord: 1.8ms)
Started GET "/tasks/new?task%5Blocate_id%5D=1" for IP at 2015-08-10 10:24:19 +0800
Processing by TasksController#new as HTML
Parameters: {"task"=>{"locate_id"=>"1"}}
Account Load (0.3ms) SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`id` = 2 LIMIT 1
Locate Load (0.2ms) SELECT `locates`.* FROM `locates`
Account Load (0.2ms) SELECT `accounts`.* FROM `accounts` WHERE (manager_id = 2)
Beacon Load (0.3ms) SELECT `beacons`.* FROM `beacons` WHERE (account_id = 2) AND (beacon_status < 3) AND (locate_id = '1')
Category Load (0.2ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1 [["id", 1]]
Category Load (0.2ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1 [["id", 2]]
CACHE (0.0ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1 [["id", 1]]
Rendered tasks/_form.html.erb (7.8ms)
Rendered tasks/new.html.erb within layouts/application (14.5ms)
Rendered common/_navbar.html.erb (0.6ms)
Rendered common/_footer.html.erb (0.0ms)
Completed 200 OK in 85ms (Views: 82.4ms | ActiveRecord: 1.5ms)