Issue regarding the change event of textfield in t

2019-05-25 02:53发布

问题:

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:

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]);
    }