2011-03-30 33 views
1

我有一個按鈕控件與模式生成的ID作爲搜索結果 這是對象ID。從動態生成的控件的jquery事件

在第一次搜索

我有3個結果:

<input type="button" id="product_0" > 
<input type="button" id="product_1" > 
<input type="button" id="product_2" > 

另一個結果建立了我2個結果

<input type="button" id="product_23" > 
<input type="button" id="product_34" > 

我怎麼能處理他們的jQuery的事件?

例如:

$("product_id").click(function(){ 

}); 

回答

4

現在live已過時,更新了答案:

如果有不動態地添加更密切的父母,用而不是文檔作爲父母選擇器

$(document).on("click", [id^=product_]", function(){ 
//Your Code to handle the click. 
}); 
使用jquery live函數。例如: 例如: $(「[id^= product _]」)。live(「click」,function(){ //您的代碼來處理點擊 });
+0

Cyber​​nate有一個更好的選擇器,所以你應該去與他的迴應。 – Sage 2011-03-30 16:39:53

+0

已過時,但作爲Google的第一個結果。 – ppumkin 2015-03-16 10:43:49

+0

@ppumkin更新了答案。 – Chandu 2015-03-16 12:17:50

1
$("product_id").live("click", function(){ 

}); 

http://api.jquery.com/live/

+0

如果我沒有混淆任何東西,那麼選擇「product_id」不會。 – ITroubs 2011-03-30 16:39:53

+0

已過時,但作爲Google的第一個結果出現。 – ppumkin 2015-03-16 10:43:51