Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
618 views
in Technique[技术] by (71.8m points)

javascript - Trigger a click with jQuery using link_to of rails 4

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' %>

this generates:

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

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!

EDIT

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

@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.

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Though its very late and you have got your problem solved, but here is the actual solution :)

$('#fixture-link')[0].click();

You missed [0] portion.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...