我在用文本框和单选按钮与他们的“实例名称” AS3闪存形式的所有设置连续,如: field_1 field_2 field_3 ...等as3 - 遍历所有文本字段?
有没有在AS3代码的简单方法循环所有这些字段,并获得他们的值在数组中?理想情况下,我想要一个简单的循环来获取值(所以我可以添加一个简单的弹出框,如果缺少一个值),然后用填充数组简单地发布它使用URLRequest。
谢谢!
我在用文本框和单选按钮与他们的“实例名称” AS3闪存形式的所有设置连续,如: field_1 field_2 field_3 ...等as3 - 遍历所有文本字段?
有没有在AS3代码的简单方法循环所有这些字段,并获得他们的值在数组中?理想情况下,我想要一个简单的循环来获取值(所以我可以添加一个简单的弹出框,如果缺少一个值),然后用填充数组简单地发布它使用URLRequest。
谢谢!
听起来你想要使用for语句。我还使用了一个多维数组来存储实例名称以及它们包含的文本。我喜欢使用变量_path来定义我的代码的范围。
var _path = this;
var text_ar:Array = new Array(['instance_1',''],['instance_2',''],['instance_3','']); //instance name, instance text.
for(var i=0; i<ar.length; i++)
{
text_ar[i][1] = _path[text_ar[i][0]].text
}
trace(text_ar[1][1]); //Traces the text that's entered in instance_1.
完美,谢谢! – Joe
如果你想要的方式,就是少一点的工作,更易于维护(如果文本的量可以在将来改变,或者你需要重复使用其他形式的代码或者不想用实例来打扰名称),那么你可以做下面的代码。
请记住,这假设所有的文本字段是相同的父的孩子,是在父母的范围(所以在保存你所有的文本字段时间轴帧)
function validateTextFields(){
var tmpTf:TextField;
var i:int = numChildren;
while(i--){ //iterate through all the display objects on this timeline
tmpTf = getChildAt(i) as TextField;
if(tmpTf){
//now that you have the textfield, you can check for an appropriate value, or send the value to a server, or store it in an array etc.
//check if the value is blank, if so set the background to red
if(tmpTf.text == ""){
tmpTf.background = true;
tmpTf.backgroundColor = 0xFF0000;
}else{
tmpTf.background = false;
}
}
}
}
@Joe - 你觉得这个回答有用吗? – BadFeelingAboutThis
有你考虑使用数据网格? –