2011-09-27 50 views
2

假設我有一個DataGrid或中繼器這樣聲明的方式設置jQuery.Data財產的HTML鏈接

<a href="javascript:void(0);" class="DoSomething">DoSomething</a> 
內我行的HTML鏈接

現在還假設我已經處理的單擊事件爲我所有DoSomethings在jQuery中是這樣的

$(".DoSomething").click(function (e) { 
    //Make my DoSomethings do something 
}); 

將數據傳遞給click事件的正確方法是依賴於單擊的鏈接?

沒有jQuery,你通常會這樣做。

<a href="javascript:void(0);" class="DoSomething" onclick="DoSomething('<%# Eval("SomeValue") %>');">DoSomething</a> 

但這種技術顯然不適用於jQuery的情況。

基本上我的理想解決方案會以某種方式爲單擊的鏈接添加jQuery.Data屬性的值,但這樣做是聲明性的。

回答

2

您的問題我不清楚,但可能是這將有助於

$(".DoSomething").click(function (e) { 
    //Make my DoSomethings do something 
    $(this).data("key","value"); 
    //later the value can be retrieved like 
    var value=$(this).data("key"); 
    console.log(value);// gives you "value" 
}); 
4

使用HTML5數據 - 屬性。 jQuery的支持是內置了1.4.3+

http://api.jquery.com/data/#data2

<a href="#" class="product-link" data-productid="77">click here</a> 

$('.product-link').click(function (e) { 
    alert($(this).data('productid')); 
});