2010-05-10 63 views
14

我有一個簡單的跨度,像這樣將數據傳遞到一個jQuery click()函數

<span class="action removeAction">Remove</span> 

這個跨度是一個表中,每一行都有一個刪除跨度。

然後我在點擊該跨度時使用AJAX調用一個URL。 AJAX事件需要知道該行對象的ID?將該ID加入點擊功能的最佳方式是什麼?

我想我可以做這樣的事情

<span class="action removeAction" id="1">Remove</span> 

但是,一個ID不應該以數字開頭?對?然後,我想我可以做

<span class="action removeAction" id="my1">Remove</span> 

然後,只需從ID中刪除「我的」部分,但那只是似乎!

下面是我的點擊事件和我的AJAX事件。

<script type="text/javascript" language="text/javascript"> 

$(document).ready(function() 
{ 

    $(".removeAction").click(function() 
    { 
     //AJAX here that needs to know the ID    
    } 
}); 

</script> 

我相信有這樣做的好方法嗎?

注:我不是在尋找

$(this).attr("id"); 

我希望能夠通過一個以上的資料片

感謝。傑克。

回答

14

如果你堅持使用老派的HTML 4.01或XHTML:

$('.removeAction').click(function() { 
// Don’t do anything crazy like `$(this).attr('id')`. 
// You can get the `id` attribute value by simply accessing the property: 
this.id; 
// If you’re prefixing it with 'my' to validate as HTML 4.01, you can get just the ID like this: 
this.id.replace('my', ''); 
}); 

順便說一句,在HTML5中,the id attribute can start with a number or even be a number

話又說回來,如果你使用HTML5,無論如何,你可能會更好過使用自定義數據屬性,像這樣:

<span class="action removeAction" data-id="1">Remove</span> 
+0

啊,自定義屬性可以嗎?我認爲他們是W3C的壞人? – 2010-05-10 10:04:58

+2

@jakenoble:他們爲什麼會這樣?它們非常棒,在像你這樣的情況下非常有用:http://www.w3.org/TR/html5/dom.html#custom-data-attribute – 2010-05-10 10:08:25

+0

這很好理解。謝謝。大拇指向上。 – 2010-05-10 10:16:10

3

$(這個)你的點擊函數中代表點擊的元素

$(".removeAction").click(function() { 
    //AJAX here that needs to know the ID 
    alert($(this).attr('id'));   
} 
+0

感謝蒂姆。這不是我正在尋找的[將編輯我的帖子]。 – 2010-05-10 10:02:54

+0

爲什麼使用'$(this).attr('id')'而不是'this.id'? – 2010-05-10 10:17:38

+0

保持它的jQuery-Way :-)我嚴格執行此操作以避免可能出現的x瀏覽器問題... – Flatlin3 2010-05-10 10:48:01

0

你可以嘗試jQuery.data():http://api.jquery.com/jQuery.data,但是這將意味着產生JS服務器端代碼執行的頁面加載時這樣就可以存儲的ID以備後用(你的刪除操作)

// this part would have to be server 'generated' 
// and by that I'm referring to the '<?=$row_id?>' 
$('table .remove').each(function(){ 

    var MyElem = $(this); 
    MyElem.data('id', <?=$row_id?>); 

    MyElem.click(function(){ 
     the_id = $(this).data('id'); 
     // ajax call goes here 
    }); 

}); 
+0

這很有趣 - *標記供以後閱讀* – 2010-05-10 10:05:30

1

下面的代碼將讓你的點擊span的ID。使用ID並不完美,但它會起作用。

$(".removeAction").click(function() { 
    alert($(this).attr("id")); 
}); 
+0

謝謝Glen。這不是我正在尋找的[將編輯我的帖子]。 – 2010-05-10 10:02:32

+0

爲什麼使用'$(this).attr('id')'而不是'this.id'? – 2010-05-10 10:04:09

+0

不知道'this.id'是否一樣 - * nice * – 2010-05-10 10:06:41

1

你可能會對存儲的ID和各行的隱藏字段/或JSON對象&所需的其他數據,而不是使用span標記進行黑客攻擊。

<tr> 
<td> 
<span class="action removeAction">Remove</span> 
<input type="hidden" value="1" /> 
</td> 
</tr> 

$('.removeAction').click(function(){ 
    var id = $(this).next().val(); 
    // do something... 
}); 

HTH

+0

謝謝。沒有想到這一點。有點*矮胖*我覺得並不像自定義屬性那麼優雅。 – 2010-05-10 10:20:13

+1

@jakenuble - 我同意解決方案的「笨重」性。該頁面可能無法驗證xHTML的自定義屬性... – Sunny 2010-05-10 10:24:57

+1

@jakenoble - 這是一個很好的地方看到/針對自定義屬性的一些參數:http://stackoverflow.com/questions/994856/so-what-if-自定義的HTML屬性,ARENT化有效的XHTML – Sunny 2010-05-10 10:36:39

相關問題