首页 > 解决方案 > 当 Ruby on Rails 中的 date_select 值更改时调用 javascript

问题描述

<%= date_select(:production_month, :date, order: [:month, :year], :start_year => @start_year, :end_year => @end_year) %>

我在我的 Rails 表单之一中有上述 date_select。

我希望在更改上述 date_selected 的值时调用 javascript 函数。

我在我的代码中编写了以下 javascript 块来处理这个问题:

$('#production_month').change(function () {alert("Yayyy!!!")});

但是无论我在 date_select 中选择什么值,这个 javascript 函数都不会被调用。

我在这里做错了什么?请帮忙!

标签: javascriptruby-on-railsdatepicker

解决方案


通过内置的 rails 方法可能更容易做到这一点:

Rails 3 & Ajax date_select onchange => 提交

date_select(object_name, method, options = {}, html_options = {})

在你的情况下:

<%= date_select(:production_month, :date, order: [:month, :year], :start_year => @start_year, :end_year => @end_year, html_options: { onchange: 'productionMonth();' } ) %>

编辑:或者使用我误读的正确方法名称:

<%= date_select(:production_month, :date, order: [:month, :year], :start_year => @start_year, :end_year => @end_year, html_options: { onchange: 'fire_on_product_change();' } ) %>

推荐阅读