Rails - 在回调中访问 AJAX 触发元素
我有一个视图,比如 show.js.erb。我在另一个视图中有一个链接,可以
link_to "MyLink", my_object_path, :remote => true
成功返回 show.js.erb 视图。我的问题是,从该视图中,是否有任何方法可以访问触发 AJAX 调用的元素,而不必生成特定于各个元素的 id ...
我希望能够使用此视图回调在单击的任何元素旁边打开一个小对话框,但我似乎找不到访问触发元素的方法。
我尝试使用 $(this) 但这不起作用。
我想做一些类似的事情
$(this).after("some new html here");
I have a view, say show.js.erb. And I have a link in another view such that
link_to "MyLink", my_object_path, :remote => true
successfully returns the show.js.erb view. My question is, from within that view, is there any way to access the element that triggered the AJAX call without having to resort to generating an id specific to individual elements a la ...
I want to be able to use this view callback to open a small dialog next to whatever element was clicked on, but I can't seem to find a way to access the triggering element.
I tried using $(this) but that doesn't work.
I'd like to do something along the lines of
$(this).after("some new html here");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的解决方案是将预提交类绑定到元素,在我的例子中是弹出模式窗口。这是与上面链接的帖子类似的解决方案,它使用预提交绑定,但适合使用类。
在 public/javascripts/application.rb 中:
然后在我看来弹出内容(例如 app/views/mymodel/popup.js.erb):
如果这看起来不合规矩,我洗耳恭听,但现在可以用。
My solution was to bind a pre-submit class to the element, in my case a popup modal window. It's a similar solution to the post linked to above in that it uses the pre-submit bindings, but tailored to use classes instead.
In public/javascripts/application.rb:
Then in my view for the popup content (e.g. app/views/mymodel/popup.js.erb):
If this doesn't look kosher, I'm all ears but it works for now.