Open link when doubleclicking on table row with jQuery

一笑奈何 提交于 2019-12-11 08:20:40

问题


I have a table that looks like this:

<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.

Any idea how to do this?


回答1:


Do you have any jQuery you've written yet? Here's a headstart...

Define your ID in the row:

<tr id="something">...</tr>

Then use something like this:

$('tr').dblclick(function(){
  var id = $(this).attr('id');
  //do something with id
})



回答2:


Working demo: http://jsfiddle.net/Xr7LC/ (created from the sample code you provided)

  1. Use dblclick api 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.

jQuery code for dblclick:

$(document).ready(function() {
    $('#table >thead > tr').dblclick(function() {
    alert('Row dblclicked');
        alert($(this).attr('class'));
    });
});​



回答3:


This may help you:

jQuery(function($) {
    $('#table tr').click(function() {
        return false;
    }).dblclick(function() {
        window.location = url;
        return false;
    });
});



回答4:


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.



来源:https://stackoverflow.com/questions/10382650/open-link-when-doubleclicking-on-table-row-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!