2011-06-25 41 views
0

我需要將參數傳遞給addEventListener的回調處理程序。我試過this,但它在for循環中不起作用。 這裏是我的代碼:將參數傳遞給鈦中的addeventlistener

var view = Ti.UI.createView({ 
    //configuration here. 
}); 
for(var i=0,ilen=response.length; i<ilen; i++){ 
    var thisObj = response[i]; 
    var btnCareer = Ti.UI.createButton({ 
     //configuration here 
    }); 

    var careerID = thisObj.CareerID; 
    btnCareer.addEventListener('click', function(){ 
                  bh.logic.career.CareerClick(careerID); 
            }); 
    view.add(btnCareer); 
} 

我所得到的是最新的值。

有什麼辦法嗎?

回答

0

我終於得到了答案here。基本上,我應該記住,JavaScript是鬆散耦合的語言。

1

沒有看到什麼響應[]包含並且如果careerID旨在是線性的,等等,下面代碼的目的是提出加入careerID作爲按鈕的實際ID屬性;可以參考的東西。

我也不知道是什麼平臺,您可以針對;-)發展的話,給下面一掄,如果一切都失敗了,請上網:

The Button API by clicking here

HTH!

var view = Ti.UI.createView({ 
    //configure the view here. 
}); 

for(var i=0,ilen=response.length; i<ilen; i++){ 
    var thisObj = response[i]; 
    var careerID = thisObj.CareerID; 
    var btnCareer = Ti.UI.createButton({ 
     //configuration here 
     id:careerID // THIS GIVES THIS BUTTON AN ID TO MAP BY EVENT LISTENER ONCLICK 
    }); 
    btnCareer.addEventListener('click', function(e){ 
     if (e.source.id !== null) { // Be good and check for null values 
      var careerIDValue = e.source.id; 
      /* e is this function, source looks at id value for the button you just 
      * created as it attaches the eventListener to the most current button 
      * object and keeps the button unique because you've given it an id: blah 
      * value that can be sussed out 
      */ 
      bh.logic.career.CareerClick(careerIDValue); 
     } else { 
      alert('NULL ID FOUND! ARGH! ' + e.source.id); 
     } 
    }); 
    view.add(btnCareer); 
} 
+0

實際上,我不得不將多個變量值傳遞給click事件,所以我寧願用當前標記的答案。 – iMatoria