2012-05-03 49 views
0

我需要找到單擊另一個圖像按鈕(在表格單元格2內)的圖像按鈕(在表格單元格1內)的id。如何使用jquery在html表中找到元素的ID

我無法在javascript/jquery中對任何索引進行硬編碼。

是否有任何其他方式來獲取該特定行內的第二個按鈕的ID?

請幫

代碼示例

<tr class="home-history-grid-row"> 
    <td> 
     <img id="test" height="16px" width="16px" onclick="changeImage(this)" src="SiteImages/deleteconfirm.png" title="Confirm Delete" alt="Delete"> 
    </td> 
    <td> 
     <input id="ctl00_Content_NotificationHistory_ctl02_ImgDelete" type="image" style="height: 16px; width: 16px; border-width: 0px;" src="SiteImages/X.gif" clientidmode="Static" disabled="disabled" name="ctl00$Content$NotificationHistory$ctl02$ImgDelete"> 
    </td> 
</tr> 
+1

u能請提供的HTML代碼? –

+0

@Umesh請找到最新的問題。 – NewBie

回答

1

如果我理解正確的話,你需要從點擊圖像找到輸入的ID? 如果是這樣,那麼這將你做它:

function changeImage(self) { 
    $(self).closest('tr').find('td input').attr('id'); 
} 
+0

現在試試你的解決方案。 – NewBie

+0

謝謝你天才。有效。 – NewBie

1
function changeImage(me){ 
    alert(me.id) 
} 
+1

我想他想要在這個函數內輸入的id,而不是圖像本身的id。 –

+0

確定在這種情況下它會是$(我).parent()。下一步()。找到(「輸入」)。attr(「id」) – Sethunath

+0

看到我的回答;) –

0

下面的代碼必須解決您的問題:

function changeImage(elm) { 
    var otherImageId = $(elm).parent().next().find("input:first").attr("id"); 

    // ... use otherImageId here ... 
} 
+0

它應該是'input:first'而不是'input:fist' - 至少如果你不想對你的輸入元素進行性騷擾。 –

+0

感謝您的更正,@DanielBaulig。 –

相關問題