正如凱文說,你需要使用類而不是一個ID。
在類的事件處理程序,在這種情況下click
,使用this
,這樣你具體指的是點擊的元素,像這樣:
$('.toggler').click(function(){
$(this).append('clicked element id: '+this.id); //will show the unique id for the toggled element
//$.post(); will want to put your $.post inside to also make use of "this"
});
爲了幫助你瞭解你也可以做此上byTagName
,在這種情況下,由表格單元格(td
):
$('td').click(function(){
$(this).append('clicked element id: '+this.id); //will show the unique id for the toggled element
//$.post(); will want to put your $.post inside to also make use of "this"
});
this
的更多用途:如果您要刪除或添加行到您的表中,並且需要跟蹤您正在處理的行,那麼您可以使用jQuery或純粹的javascript內部click事件,如下所示: 以顯示哪些數字行,你點擊:
$("table tr").click(function(){
alert('jQuery: '+$(this).index()); //jQuery
alert('javascript: '+this.rowIndex); //javascript
});
最後,如果當你需要使用jQuery的on()
方法使用事件委派頁面加載不存在的行。這也可能是您不能單擊除第一行之外的其他行的原因。
$(document.body).on('click', "table tr", function(){
alert('jQuery: '+$(this).index()); //jQuery
alert('javascript: '+this.rowIndex); //javascript
});
你能分享你正在嘗試的代碼嗎?如果通過Id表示HTML標識,則這些應該是每頁一個,所以您可能想要使用類。 – 2013-03-18 14:50:42
是的,不允許在HTML文檔中多次設置一個ID(這非常重要)。 – Eich 2013-03-18 14:51:12