我需要将参数传递给addEventListener的回调处理程序。我尝试了this,但它在 for 循环中无效。 这是我的代码:
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 :(得分:1)
如果没有看到[]包含什么样的响应,并且如果businessID是线性的等等,则以下代码的目的是建议将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);
}
答案 1 :(得分:0)
我终于得到了答案here。基本上,我应该记住,javascript是松散耦合的语言。