简体   繁体   English

用jQuery单击rails link_to

[英]Click a rails link_to with jQuery

I'm trying to click a rails ajax link using jquery by clicking on a google map marker but can't figure out how to do it. 我试图通过单击谷歌地图标记单击使用jquery的Rails Ajax链接,但不知道如何做到这一点。 Here's the link : 这是链接:

sidelistings div

<% @properties.each_with_index do |property,index| %>
          <div class="sidelisting" id="<%= index %>">
            <div><%= link_to image_tag("http://432.mlsimages.movoto.com/0#{property.mls.last(2)}/tn/#{property.mls}_0.jpg"), {:action => 'show', :id => property.id}, :remote => true %>  </div>

I have a google map with 20 markers on it which match the 20 properties in the above sidelistings div. 我有一张上面有20个标记的google map,它与上述sidelistings div中的20个属性匹配。 I want to be able to click on the map marker and that will click the above link for it's corresponding property. 我希望能够单击地图标记,这将单击上面的链接作为其对应的属性。 I have made the id s of the 20 sidelistings match the indexes of the array of map markers. 我已将20个辅助列表的id匹配到地图标记数组的索引。 Here's my attempt at the jquery in the google maps code: 这是我在Google Maps代码中的jQuery尝试:

google.maps.event.addListener(markerArray[i], 'click', function(){
         var element = this.id;
         var sidelisting = document.getElementById(element);
         $('sidelisting:a').click();
     });

The above code doesn't throw any errors in the console but clicking on the map marker does nothing, what am I doing wrong? 上面的代码在控制台中没有引发任何错误,但是单击地图标记没有任何作用,我在做什么错?

Got it figured out, I was getting a couple of things wrong, here's the working code: 弄清楚了,我错了很多,这是工作代码:

google.maps.event.addListener(markerArray[i], 'click', function(){
         var index = markerArray.indexOf(this);
         $('#' + index + ' a').click();
});

I didn't know I had to construct the selector within the $() that way with the plus signs and the space before the a tag is also required 我不知道我是否必须在$()内以加号和还需要a标记的空格来构造选择器

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

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