Issue regarding the change event of textfield in t

2019-05-25 02:41发布

I have the following code

for(var i=0; i<3; i++){
var newTF = Ti.UI.createTextField({
    top : 20,
    hintText : "new text field under "+(i+1)+" text field"
    width : '100%',
    id:args.obj.fields[i].id
});

   $.textFieldView1.add(newTF);
 }

  newTF.addEventListener('change',function(e){
    if(e.source.getValue=='entered value'){
      //do something
    }
    else{
      //do something
    }
});

Now the problem which I am facing is I am getting the last created textfield(i.e id of last created textfield).So I am not able change trigger any changes on previously created textfields.Its like previous id of textfield is overlapped by latest one.

Can anyone help on this? Thanks in advance

1条回答
祖国的老花朵
2楼-- · 2019-05-25 03:33

Try this:

var newTF = [];
    for (var i = 0; i < 3; i++) {
        newTF[i] = Ti.UI.createTextField({
            //top : 20,
            hintText : "new text field under " + (i + 1) + " text field",
            width : '100%',
            id :i+1,
        });
        //newTF[i].addEventListener('');
        newTF[i].addEventListener('change', function(e) {
            Ti.API.info('e is'+JSON.stringify(e));
         if (e.source.getValue == 'entered value') {
                // //do something
         } else {
                // //do something
         }
        });
        $.laynout.add(newTF[i]);
    }
查看更多
登录 后发表回答