2011-06-20 35 views
1
<tr> 
       <td style="display:none;"> 
        62 
       </td> 
       <td> 
        <span style="color:Blue; cursor:pointer;" id="editLink">Edit</span> | 
        <span style="color:Blue; cursor:pointer;" id="detailsLink">Details</span> 
       </td> 
       <td> 
        Update Loan Data Component Rows 
       </td> 
       <td> 
        Daily: 22:00:00 PM 
       </td> 
       <td> 
        False 
       </td> 
      </tr> 

所以我有一個綁定到cursor:pointer的兩個跨度的JQuery單擊事件。點擊時,我希望能夠輕鬆獲取此行中每個表格單元格中的值,並使用這些值在頁面的其他位置設置元素。用jQuery獲取同一行的值

我該如何在JQuery中做到這一點?

P.S.我可以記住不同的單元格,但是每行都會有複製代碼。

回答

3

在附接至所述<span>元件click處理程序:

$(this).parent().siblings().each(function() { 
    alert('index ' + this.cellIndex + ': ' + $(this).text()); 
}); 

編輯:添加cellIndex屬性的警報,以指示<td>單元格的索引號。

例子:http://jsfiddle.net/xjFGY/

+0

這很完美,但是,我怎麼知道我在哪? – slandau

+0

@slandau:你什麼意思?你想要什麼類型的指標,除了文字內容?像索引一樣? – user113716

+0

完全像索引 – slandau

3
$("span").click(function() { 
    var $tr = $(this).parents("tr"); 
    alert($tr.find("td").eq(0).html()); // 1st td (62) 
    alert($tr.find("td").eq(2).html()); // 3rd td 
    alert($tr.find("td").eq(3).html()); // 4th td (date/time) 
    alert($tr.find("td").eq(4).html()); // 5th td (bool) 
}); 
0

你應該儘量具有唯一的ID。如果你有多個,請使用類。

$('span#editLink').click(function(){ 
$(this).parent().siblings().each(function(){ 
    if($(this).attr('class') == "frequency"){ 
     $('#other-frequency-field').html($(this).text()); 
    } 
    else if($(this).attr('class') == "boolean"){ 
     $('#other-frequency-field').html($(this).text()); 
    } 
} 

有了這個,你必須添加類值到其他表列以識別它們。 (我用布爾和頻率,就像例子)。