【问题标题】:actionscript 3 - use textfield text in for loopactionscript 3 - 在 for 循环中使用文本字段文本
【发布时间】:2013-05-29 15:12:44
【问题描述】:

我在舞台上有 5 个动态文本字段。它们的实例名称为 txtField_1txtField_2txt_Field_5

如果我想追踪其中的数据,我会写:

trace(txtField_1.text);
trace(txtField_2.text);
.
.
.
trace(txtField_5.text);

如果我想在 for 循环中执行此操作怎么办?

我正在寻找类似的东西:

for (i=1 ; i<=5 ; i++)
{
   trace(txtField_[i].text);
}

【问题讨论】:

    标签: actionscript-3 textfield trace


    【解决方案1】:

    您可以使用this[ "variableName" ] 语法。

    var fields:uint = 5;
    
    for ( var i:uint = 1; i <= fields; i++ ) {
        trace( this[ "txtField_" + i ].text ); //you may need to cast as a TextField to avoid compiler errors
    }
    

    this["variableName"] 与访问this.variableName 相同,但它允许您在变量名中使用变量。

    【讨论】:

      【解决方案2】:

      如果您的 TextField 位于数组中,则可以这样做。

      var _textFields:Array = new Array();
      _textFields.push(new TextField());
      _textFields.push(new TextField());
      _textFields.push(new TextField());
      _textFields.push(new TextField());
      _textFields.push(new TextField());
      _textFields[0].text = "text field 1 text";
      _textFields[1].text = "text field 2 text";
      _textFields[2].text = "text field 3 text";
      _textFields[3].text = "text field 4 text";
      _textFields[4].text = "text field 5 text";
      

      然后:

      for (var loop:uint=0;loop<_textFields.length;loop++) {
          trace(_textFields[loop].text);
      }
      

      【讨论】:

      • 不幸的是,文本字段在舞台上,而不是在数组中。你还有什么建议吗?
      • @MiladGhattavi 你可以...将它们放入一个数组中。
      【解决方案3】:

      另一种方法:

      private function getTextFields():void{
          for(var i:uint = 0; i < stage.numChildren; i++){
              if(stage.getChildAt(i) is TextField){
                  // Whatever needs to be done with the text. e.g. putting it to an array ;);
                  trace(TextField(stage.getChildAt(i)).text;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-02
        • 2013-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多