2011-04-12 45 views
0

我有來自於服務這個對象結構:使用WHERE方法搜索對象?

var response ={"results": [ 
{"name":"peter" , "habits":"smoking" , "imgID":"something_unique"}, 
{"name":"paul" , "habits":"drinking" , "imgID":"something_unique"}, 
{"name":"patrick" , "habits":"gambling" , "imgID":"something_unique"} 
]} 

我迭代的對象,並創建它們通過點擊事件聽過的div:

for(var prop in response) { 
     if(response.hasOwnProperty(prop)) 
      {  
this.div.insert(new Element('div', {'className': '' }) 
.insert('<img id="'+imgID+'" src="/img.jpg" width=70 height=70 /> ') 
        ); 

在我的回調,我可以訪問從對象ID催生事件:

function callback (event) { console.log(event.id) ; var compare_ID=event.id } 

然而,在我的回調,我需要爲一個特定的對象,這是獲得數組變量名稱點擊。因此,在概念,我希望做:

在回調,迭代response對象,然後選擇results[i]它是imgID是進一步訪問== event.id return response對象,而不僅僅是名。

感謝,並像往常一樣,所有的幫助表示讚賞

回答

0

你可以使用data-* attributes存儲數量或物體的關鍵,或實際值。我認爲這會有所幫助。

例如爲:

<img id="something_unique" src="/img.jpg" width=70 height=70 data-result-number="1" /> 

<img id="something_unique" src="/img.jpg" width=70 height=70 data-name="peter" data-habits="smoking" /> 
+0

你如何通過數據集中到一個事件的回調? – cube 2011-04-12 21:28:23

+0

例如'results [event.target.getAttribute('data-result-number')]',或者像第二種情況那樣直接從元素'event.target.getAttribute('data-name')'中取值。這些值附加到元素節點,因此您可以通過使用'event.target'訪問元素來訪問它們。 – pepkin88 2011-04-12 21:35:18

+0

謝謝,得到它使用你的方法工作。 – cube 2011-04-12 22:22:52