2013-03-04 33 views
2

我正在使用mvc4和jquery。获取jQuery中的双击事件表的rowid

我正在试图找到一种方法来创建整个行来激发doubleclick事件。

我的场景是,包含参数Person ID,FirstName和Lastname的模型。

但是html表格只显示FirstNmae和Lastname。

在哪里存储的人员ID,如果它隐藏它不会得到JavaScript和空间问题也。

我的需求是,让行可以双击并且时间得到personID,其次是从最终用户隐藏person id。

为了得到PERSONID我用,

$('#EditTable td').dblclick(function() { 
    $(this).find('td:eq(1)') 
    $(this).find('td:first').text() 
} 

但没有得到任何价值。

+0

顺便说一句,你一定要明白你都不'find'电话做什么吗?另外,不要忘记用')关闭你的代码;' – musefan 2013-03-04 10:34:44

回答

8

解决方案

的问题是,该事件被解雇的td元素,所以thistd元素。您需要获得父母tr,然后致电find函数。

事情是这样的:

$('#EditTable td').dblclick(function() { 
    var $this = $(this); 
    var row = $this.closest("tr"); 
    row.find('td:eq(1)'); 
    row.find('td:first').text(); 
}); //<-- NOTE: you dont close correctly in your example, which cause console errors 

Here is a working example.


或者,您可以分配事件到tr元素代替,这将让你保持你原来的功能代码.. 。

$('#EditTable tr').dblclick(... 

个人认为,我倾向于使用data属性来存储“元数据”类型的东西。例如:

$('#EditTable td').dblclick(function() { 
    var $this = $(this); 
    var row = $this.closest("tr"); 
    var id = row.data("id"); 
}); 

与下面的HTML表:

<table id="EditTable"> 
    <tr data-id="1"> 
     <td>1</td><td>One</td> 
    </tr> 
</table> 

Here is a working example using data attributes.

+1

非常感谢。它的工作。 – user2017909 2013-03-04 10:57:58

0

你必须写:

// You want event fired on row `tr` and not on `td` 
$('#EditTable tr').dblclick(function() { 
    $(this).find('td:eq(1)') 
    $(this).find('td:first').text() 
} 

编辑

最好的办法是:

$('#EditTable').on(dblclick, 'tr', function() { 
    var $rowClicked = $(this); 
    var secondCell = $rowClicked.find('td:eq(1)'); 
    var textOfFirstCell $rowClicked.find('td:first').text(); 
} 
0

试试这个

$('#EditTable tr').dblclick(function() { 
    var $this=$(this); 
    $this.find('td:first').text(); //get personid 
    $this.find('td:first').hide(); //hide the personrow... u can use remove() 
}); 
0

试试这个:

$('#EditTable tr').dblclick(function() { 
    $(this).find('td:eq(1)') 
    $(this).find('td:first').text() 
}); 

试图通过TRs迭代来代替,而可能是这是一个错字或你有什么关闭时出错应该是});

1

实际上,您可以使用$.data('personID',1)方法将模型详细信息存储在DOM树中。通过这种方式,您可以隐藏用户的personID。当用户DBL点击一排,得到相应的personID$.data('personID')

//setting the personID 
$.data('personID',2); 

//getting the personID 
$.data('personID'); //returns 2 
+0

如何在剃须刀视图表中设置此值? – user2017909 2013-03-04 12:45:25

+0

@ Html.Hidden(“PhoneId”,@ item.PhoneId) – user2017909 2013-03-04 14:32:57