CoffeeScript函数未触发

时间:2015-02-18 10:00:33

标签: ruby-on-rails ruby-on-rails-4 coffeescript

我有一个由显示页面内的循环生成的输入。他们都有相同的name。当它们没有聚焦时,我想调用一个JS脚本。但是,信号永远不会被触发。为什么呢?

expenses.js.coffee

alert 'Test' // This instruction is detected

$("input[value='nb_kms[]']").blur -> // This one is not
   alert 'Handler for .blur() called.'
return

show.html.erb

<% @calendar.each_with_index do |d, i| %>       
  <tr>
    <td><input type="number" name="nb_kms[]" value="<%= @nb_kms[i] %>"></td>
  </tr>

1 个答案:

答案 0 :(得分:2)

你试过这个吗?

$("body").on "blur", "input[value='nb_kms[]']", ()->

或执行此操作,将class添加到输入

<tr>
  <td><input type="number" name="nb_kms[]" value="<%= @nb_kms[i] %>" class="kms_input"></td>
</tr>

在js

ready = ()->
  $("body").on "blur", ".kms_input", ()->
    alert $(this).val()
    alert 'Handler for .blur() called.'

$(document).ready ready
$(document).on 'page:load', ready