使用rails 4的link_to触发jQuery单击 [英] Trigger a click with jQuery using link_to of rails 4

查看:102
本文介绍了使用rails 4的link_to触发jQuery单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Rails 4应用程序。我需要使用javascript(或jQuery)触发链接中的单击。我在我看来有这个:

I'm working on a Rails 4 app. I need to trigger a click in a link using javascript (or jQuery). I have this in my view:

<%= link_to t('.fixture'), fixture_manager_tournament_path(format: :js), remote: true, id: 'fixture-link' %>

这会产生:

<a id="fixture-link" data-remote="true" href="/manager/tournaments/1/fixture.js">Fixture</a>

注意 remote:true
单击链接时工作正常,但我需要模拟点击js。
我试过:

Notice the remote: true. This is working fine when I click the link, but I need to simulate the click through js. I've tried with:

$('#fixture-link').click();
$('#fixture-link').trigger('click');
$('#fixture-link').trigger('click.rails');

但它们都没有工作。
提前致谢!

But none of them are working. Thanks in advance!

编辑

@RustComet Comet It应该替换我的一个div的HTML ...

@RustComet Comet It should replace the HTML of one of my divs...

@RichPeck我的视图太大了。然后,为了避免漫长的渲染时间,我试图通过JS加载部分。我已经使用了这个信息: https://stackoverflow.com/a/15174908/3893506 来实现这一目标。

@RichPeck I have a view too big. Then, to avoid long rendering time, I'm trying to load partials through JS. I've used this info: https://stackoverflow.com/a/15174908/3893506 to achieve that.

我的视图有各种标签,所有标签都是通过JS加载的(除了一个)。然后我在路由中添加了一个附加参数,以便直接访问所需的选项卡。所以我可以去 / tournament?active = fixture 并查看灯具。

My view have various tabs and all of they are loaded through JS (except one). Then I added an additional parameter to the route in order to access directly to the desired tab. So I can go to /tournament?active=fixture and see the fixture.

问题是,标签处于活动状态,但内容不在此处。 (当然,直到我链接上的clic才会触发ajax请求和回调)。这就是为什么我试图模拟该链接上的clic并加载相应的选项卡。

The issue is, the tab is active but the content isn't here. (Of course, the ajax request and callbacks aren't fired until I clic on the link). That is why I'm trying to simulate a clic on that link and load the corresponding tab.

推荐答案

解决了!我已经通过加载正确的JS并使用'active'参数直接执行它而无需点击链接来解决这个问题。
感谢所有帮助过我的人!

Solved! I've solved this by loading the correct JS and executing it directly, using the 'active' parameter and without need a click on the links. Thanks to everyone who helped me!

如果有人想知道,这是我用过的代码(它的coffeescript):

If someone wants to know, this is the code I've used (its coffeescript):

tournament = $('#main-wrapper').data('tournament')
$.get '/manager/tournaments/' + tournament + '/fixture.js', (data) -> data

这篇关于使用rails 4的link_to触发jQuery单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆