Rails 3-如何使用jQuery使整个表行可点击 [英] Rails 3 - How to make entire table row clickable using jQuery
问题描述
我有一个私人消息传递系统,该系统显示所有收到的消息的收件箱"视图.此视图只是通过使用视图中的块创建的多个表行生成的.
I have a private messaging system that displays an "inbox" view of all received messages. This view is simply generated from multiple table rows created by using a block in the view.
我想使每个表行都可点击",并且还要传递一个参数(就像我在使用标准的link_to一样).但是,您可能知道,无法像使用div一样使tr可点击.
I would like to make each table row "clickable" and also pass a parameter (just as if I was using a standard link_to). However, as you may know, you cannot make a tr clickable like you can with a div.
因此,我相信该解决方案将需要一些简单的jQuery.我已经找到了该问题的部分答案,但还没有一个完整的答案.
Therefore, I believe the solution will require some simple jQuery. I have found some partial answers to this question, but not a complete one.
我知道下面的代码是不正确的(link_to部分),但是有人可以告诉我如何使用jQuery click函数连接rails link_to链接吗?
I know the code below is NOT correct (the link_to part), but can someone please show me how to hook up a rails link_to link using a jQuery click function?
谢谢!
$('tr').click(function() {
<%= link_to :controller => "messages", :action => "show", :id => msg.convo_id %>
});
推荐答案
这是我发现的 http://www.electrictoolbox.com/jquey-make-entire-table-row-clickable/似乎运行良好(注意:"conver"是每个类的类名)表格行).
This is what I found http://www.electrictoolbox.com/jquey-make-entire-table-row-clickable/ and it seems to work perfectly (note: 'conver' is the class name of each table row).
$('.conver').click(function() {
var href = $(this).find("a").attr("href");
if(href) {
window.location = href;
}
}); // end
此外,我的收件箱"视图在每行中都有复选框,我可以用来选择特定邮件并触发某些选项(例如保存,删除等).我需要能够选中这些复选框,而无需触发指向信息.因此,我添加了以下代码:
Additionally, my "inbox" view has checkboxes in each row that I use to select a particular message and fire certain options such as save, delete, etc. I needed to be able to check these checkboxes WITHOUT firing the link to the message. Therefore, I added the following code:
$(':checkbox').click(function(event) {
event.stopPropagation();
}); // end
希望这对尝试生成大多数邮件系统通用的相同功能的人有帮助.
Hope this helps anyone trying to generate these same features that are common to most messaging systems.
这篇关于Rails 3-如何使用jQuery使整个表行可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!