简体   繁体   English

rails link_to helper-提供onclick属性可消除:remote => true(动作作为HTML处理)

[英]rails link_to helper - supplying an onclick attribute gets rid of :remote => true (action is processed as HTML)

So yeah as the title says, whenever I supply an onclick parameter to my link, it processes the resulting action as HTML in place of JS. 是的,正如标题所述,每当我向链接提供onclick参数时,它就会将结果操作作为HTML代替JS处理。 Any ideas? 有任何想法吗?

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

You could have a separate script that binds your custom event handler: 您可以有一个单独的脚本来绑定您的自定义事件处理程序:

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

You'll need to add the '.share-link' class to all the links you want to act like that, but once you've done that it should work like you want. 您需要将'.share-link'类添加到您要像这样操作的所有链接中,但是一旦完成,它应该可以按照您的意愿工作。 This method will have jQuery (which I assume you're using, from your code sample) bind the event handler, leaving the 'onclick' attribute free for Rails to use for AJAX purposes. 此方法将使jQuery(我假设您正在使用的代码示例中的jQuery)绑定事件处理程序,并为Rails保留“ onclick”属性供Rails用于AJAX。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM