为什么我必须重新加载页面才能使点击回调正常工作?
使用 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/