开发者

jquery onclick function not firing with rails link_to_remote

In the js file of the page, inside $(document).ready(function() {}) I have

$(".school a").live("click", function (e){ 
  e.preventDefault();
  ....;
  jsFunc(param1, param2, param3);
});

Now the div with the class school has tags generated by rails link_to_remote with :url, :action, :before, :html.

On clicking on this link it does all that it should do with regards to link_to_remote, but somehow the onclick event in the document.ready does not attach to it. Why would this be happening? The jsFunc all it does is post to a url async-ly, i figured out that stuffing that post url in the :before of the link_to_remo开发者_如何转开发te would work - but is there a more elegant way of just being able to use the attach functionality


I believe the link_to_remote helper depends on Prototype; if you've switched to jQuery, Rails's javascript helpers such as link_to_remote might not work.


link_to_remote works with the onclick attribute. Apparently these get called before any bind/live events are processed, so your preventDefaults happens too late.

In my case I just want to prevent the 'second' click on the link, so I just nulled the "onclick" attribute during the first click:

/* not (yet) well tested */
function doubleClickhandler(event) {
  var t = $(event.target);

  if (t.data("clicked")) {
    event.preventDefault();
  } else {
    t.data("clicked", true);
    if (t.attr("onclick")) {
      /* forcefully remove onclick handler */
      t.attr("onclick", "");
    }
  }
}

function setupdoubleClickHandlers() {
  $("a.doubleclick").each(function () {
    var el = $(this);
    el.removeClass("doubleclick"); /* prevent multiple setups */
    el.bind("click", doubleClickhandler);
  });
}

jQuery(document).ready(function($) {
  setupdoubleClickHandlers();
  $(document).bind('reveal.facebox updatedfacebox', function() {
    setupdoubleClickHandlers();
  });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜