Javascript 使用 jQuery 双击表格行时打开链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10382650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open link when doubleclicking on table row with jQuery
提问by nimrod
I have a table
that looks like this:
我有一个table
看起来像这样:
<table id="table">
<thead>
<tr class='tablehead'>
<th>Test</th>
</tr>
</thead>
<tbody>
<tr class='tablecell'>
<td>
</td>
</tr>
</tbody>
</table>
- I want to be able to double click on a row and then trigger a link.
- An ID has to be transmitted somehow. Where should I define this? This allows me to edit the selected row afterwards.
- 我希望能够双击一行然后触发一个链接。
- 必须以某种方式传输 ID。我应该在哪里定义这个?这允许我之后编辑选定的行。
Any idea how to do this?
知道如何做到这一点吗?
回答by LeonardChallis
Do you have anyjQuery you've written yet? Here's a headstart...
你有没有写过的jQuery?这是一个先机...
Define your ID in the row:
在行中定义您的 ID:
<tr id="something">...</tr>
Then use something like this:
然后使用这样的东西:
$('tr').dblclick(function(){
var id = $(this).attr('id');
//do something with id
})
回答by Tats_innit
Working demo:http://jsfiddle.net/Xr7LC/(created from the sample code you provided)
工作演示:http : //jsfiddle.net/Xr7LC/(根据您提供的示例代码创建)
Use
dblclick
api http://api.jquery.com/dblclick/You can use
$(this).attr('id')
to get the id, and obviously you will define the id in a tag.
使用
dblclick
api http://api.jquery.com/dblclick/您可以使用
$(this).attr('id')
来获取 id,显然您将在标签中定义 id。
jQuery code for dblclick:
dblclick 的 jQuery 代码:
$(document).ready(function() {
$('#table >thead > tr').dblclick(function() {
alert('Row dblclicked');
alert($(this).attr('class'));
});
});?
回答by Sudhir Bastakoti
Do you mean something like this:
你的意思是这样的:
$(document).ready(function() {
$('.tablecell').click(function() {
return false;
}).dblclick(function() {
window.open("your_url");
return false;
});
});
and you could create a hidden field and populate that field with the id when double clicked.
并且您可以创建一个隐藏字段并在双击时使用 id 填充该字段。
回答by arun
This may help you:
这可能会帮助您:
jQuery(function($) {
$('#table tr').click(function() {
return false;
}).dblclick(function() {
window.location = url;
return false;
});
});