onclick - 为什么 onclick 在 Rails4 和 coffeescript 中使用 turbolinks 的第二页请求不起作用?

标签 onclick coffeescript ruby-on-rails-4

为什么我必须重新加载页面才能使点击回调正常工作?

使用 Rails 4.0.0.rc2 我想点击 table-row/table-td 而不是标准链接。 表格行有一个 css 类,当点击该行时, CoffeeScript 应该加载正确的 url。

下面显示的代码第一次运行良好并重定向到正确的目标。 然后,当我单击菜单(使用 turbolink 处理)以显示原始表格时,除非我重新加载表格,否则点击任何表格行都不起作用。

我的 haml-template 看起来像这样

....
%tr{class: 'clickable-row', id: 'row-id-1'}
  %td Some text
  %td more text
%tr{class: 'clickable-row', id: 'row-id-2'}
  %td ...
  %td ...

CoffeeScript :

jQuery ->
  $('.clickable-row td').click ->
    target_id = $(this).parent().attr('id').replace /row-id-/,''
    document.location = "/controller_xy/#{target_id}/edit"

最佳答案

虽然,这不是“为什么”问题的答案,但至少是一种解决方法。

工作的 coffeescript 看起来像这样:

jQuery ->

  $(document).on( 'click', '.clickable-row td', ->
    target_id = $(this).parent().attr('id').replace /row-id-/, ''
    document.location = "/controller_xy/#{target_id)}/edit"
  )

关于onclick - 为什么 onclick 在 Rails4 和 coffeescript 中使用 turbolinks 的第二页请求不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17133260/

相关文章:

android - 共享首选项或文件保存 24 editTexts

javascript - 单击图像更改图像和调用功能

javascript - 检查边界的更好方法

ruby-on-rails - Rails 关联未正确加载

javascript - 为表格中的每一行添加 onclick 事件

javascript - 当实际的鼠标单击未在目标元素上开始时,如何防止触发单击事件?

jquery - Ajax 附加对象在 dom 中不可见

node.js - 使用 CoffeeScript 从 node.js 上的 Redis 中提取对象数组的更好方法

rspec-rails - Rails 4 数据库清理器

javascript - 将浮点十六进制转换为 Ruby 中的 float