2011-02-28 50 views
6

我在我的MVC應用程序中有一個表。我想獲取選定行的id。我使用jQuery捕獲tr的click事件。該表樣本如下所示獲取表中所選行的id -HTML

<table id="resultTable"> 
<tr id="first"> 
    <td>c1</td>  
    <td>c2</td>  
</tr> 
<tr id="second"> 
    <td>c3</td>  
    <td>c4</td>  
    </tr>  
</table> 

我使用下面的腳本來訪問行單擊事件

$(document).ready(function() {  
    $('#resultTable tr').click(function (event) { 
      alert(this.); //trying to alert id of the clicked row   

    }); 
}); 

但它不是working.How可以得到選擇行ID ??任何想法?

回答

18
$(document).ready(function() {  
    $('#resultTable tr').click(function (event) { 
      alert($(this).attr('id')); //trying to alert id of the clicked row   

    }); 
}); 
6

你幾乎在那裏。直接訪問它:

alert(this.id);