从集合循环中获取表单字段值

时间:2012-08-09 17:53:23

标签: ajax forms loops collections coldfusion

我正在循环一个集合(表单)并在表单字段名称中测试'attachedFile'。成功之后,我想将表单字段值添加到数组中。目前,我只获取表单字段名称而不是值。

<cfloop collection="#FORM#" item="field">
    <cfif FindNoCase('attachedFile',field) IS 1>
        <cfset fileNamesArray[fileNamesIndex] = field>
        <cfset fileNamesIndex = fileNamesIndex + 1>
    </cfif>
</cfloop>

我尝试将索引[whatever]中的数组设置为#form.field#,但这会导致错误(未定义)。任何想法如何在这个循环中获得我的价值?感谢。

2 个答案:

答案 0 :(得分:2)

<cfloop collection="#Form#" item="field">
    <cfset currentFieldName  = field>
    <cfset currentFieldValue = Form[field]>
</cfloop>

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7fe2.html

或者,如果您更喜欢脚本样式,并且您使用的是CF9,请使用for-in循环

<cfscript>
    for (field in Form)
    {
        currentFieldName  = field;
        currentFieldValue = Form[field];
    }
</cfscript>

答案 1 :(得分:1)

在Coldfusion 10或Railo 4中,您可以使用cfscript中filter()Underscore.cfc library函数,如下所示:

var fileNamesArray = _.filter(form, function (value, field) {
    return FindNoCase('attachedFile', field);
});

filter()函数返回一个通过真值测试的值数组,在本例中是FindNoCase(...)。

使用功能样式编程可以产生更优雅和富有表现力的解决方案。

(注意:我写了Underscore.cfc)