简体   繁体   English

如何在onclick事件中使用Javascript调用“ link_to”

[英]How to call a “link_to” in Javascript on a onclick event

I'm trying to get the rails-'link_to' funcionality in my Javascript (jQuery). 我正在尝试在我的Javascript(jQuery)中获得rails-'link_to'功能。 In my Javascript I'm waiting for a onclick event and fetch some data via ajax. 在我的Javascript中,我正在等待onclick事件并通过ajax获取一些数据。 Afterwards I would like to call a 之后,我想打电话给

= link_to "Show", @mymodel, :remote => true, :id => "#{some.id}", :format => :js

But I cant figure out how to put it in JS. 但我不知道如何将其放入JS。 Thanks a ton for your help! 多谢您的协助!

If you are using jquery you can attach a click event on the anchor tag after it has been created with your RoR code, like so 如果您使用的是jquery,则可以在使用RoR代码创建了锚事件后将click事件附加到锚事件标签上,如下所示

$(document).ready(function(){
    $('#theid').click(function(){
        //do something here
    });
});

RJS is what you need: 您需要RJS

Create a file app/views/mymodel/show.js.erb with your JS code. 使用您的JS代码创建一个文件app / views / mymodel / show.js.erb。 And then add this code to the show method of your controller: 然后将此代码添加到控制器的show方法中:

respond_to do |format| 
  format.js
end

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

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