【问题标题】:get form field value from collection loop从收集循环中获取表单字段值
【发布时间】:2012-08-09 17:53:23
【问题描述】:

我正在遍历一个集合(表单)并测试表单字段名称中的“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#,但这会导致错误(未定义)。任何想法如何在这个循环中获得我的价值?谢谢。

【问题讨论】:

  • 仅供参考,如果您只是将内容插入数组,则可以使用 ArrayAppend()
  • FWIW - 错误可能是因为您没有在循环之前初始化索引变量。然而,正如亨利所说,arrayAppend 要简单得多,即arrayAppend(fieldNamesAray, FORM[field]);

标签: ajax forms loops collections coldfusion


【解决方案1】:
<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>

【讨论】:

  • 如何将 cfdump 设置为等于变量?
  • cfdump 只是向您说明可以使用Form[field] 访问字段值的一种方式。要将其设置为 var,请使用 &lt;cfset x = Form[field]
【解决方案2】:

在 Coldfusion 10 或 Railo 4 中,您可以像这样在 cfscript 中使用 Underscore.cfc libraryfilter() 函数:

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

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

使用函数式编程会产生更优雅和更具表现力的解决方案。

(注意:我写的是Underscore.cfc)

【讨论】:

    猜你喜欢
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2018-09-06
    • 1970-01-01
    相关资源
    最近更新 更多