jquery onclick 函数未使用 Rails link_to_remote 触发

发布于 2024-08-30 11:11:05 字数 542 浏览 4 评论 0原文

在页面的 js 文件中, $(document).ready(function() {})

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

现在有班级 school 的 div 具有由 Rails link_to_remote 使用 :url 生成的标签:动作:之前:html

单击此链接时,它会执行与 link_to_remote 有关的所有操作,但不知何故, document.ready 中的 onclick 事件不会附加到它。为什么会发生这种情况呢? jsFunc 所做的就是异步发布到 url,我发现将该发布 url 填充到 link_to_remote 的 :before 中可以工作 - 但有没有一种更优雅的方式能够使用附加功能

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_remote would work - but is there a more elegant way of just being able to use the attach functionality

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一百个冬季 2024-09-06 11:11:05

我相信 link_to_remote 助手依赖于 Prototype;如果您已切换到 jQuery,Rails 的 javascript 帮助程序(例如 link_to_remote)可能无法工作。

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.

鯉魚旗 2024-09-06 11:11:05

link_to_remote 与 onclick 属性一起使用。显然,这些在处理任何绑定/实时事件之前被调用,因此您的 PreventDefaults 发生得太晚了。

就我而言,我只是想防止“第二次”点击链接,因此我只是在第一次点击期间将“onclick”属性设为空:

/* 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();
  });
});

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();
  });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文