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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:06:32  来源:igfitidea点击:

Open link when doubleclicking on table row with jQuery

javascriptjqueryhtmldouble-click

提问by nimrod

I have a tablethat 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>
  1. I want to be able to double click on a row and then trigger a link.
  2. An ID has to be transmitted somehow. Where should I define this? This allows me to edit the selected row afterwards.
  1. 我希望能够双击一行然后触发一个链接。
  2. 必须以某种方式传输 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/(根据您提供的示例代码创建)

  1. Use dblclickapi http://api.jquery.com/dblclick/

  2. You can use $(this).attr('id')to get the id, and obviously you will define the id in a tag.

  1. 使用dblclickapi http://api.jquery.com/dblclick/

  2. 您可以使用$(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;
    });
});