rails link_to helper - 提供 onclick 属性会摆脱 :remote => true(操作作为 HTML 处理)



所以是的,正如标题所说,每当我提供一个onclick参数到我的链接,它处理结果的动作作为HTML代替JS。什么好主意吗?

link_to "/profile/share?id=#{post.id}", :id => "share_#{post.id}", :remote => true do "stuff" end
link_to "/profile/share?id=#{post.id}", :onclick => "javascript:$('#share_#{post.id}').text('Loading...')", :id => "share_#{post.id}", :remote => true do "stuff" end

您可以有一个单独的脚本来绑定您的自定义事件处理程序:

$('a.share-link').click(function() {
    $(this).text('Loading...');
});

您需要添加'。将Share-link类添加到所有你想要的链接中,但一旦你完成了,它就会像你想要的那样工作。这个方法将有jQuery(我假设你正在使用,从你的代码示例)绑定事件处理程序,留下'onclick'属性自由Rails用于AJAX目的。

希望这对你有帮助!

最新更新