簡體   English   中英

從收集循環獲取表單字段值

[英]get form field value from collection loop

我正在遍歷一個集合(表單),並測試表單字段名稱中的“ 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#,但這會導致錯誤(未定義)。 任何想法如何在這個循環中獲得我的價值? 謝謝。

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

http://help.adobe.com/zh_CN/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172172e0811cbec22c24-7fe2.html

或者,如果您更喜歡腳本樣式,並且使用的是CF9,請使用for-in循環

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

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

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

filter()函數返回一個通過真值測試的值數組,在本例中為FindNoCase(...)。

使用函數式風格的程序設計可以產生更優雅,更富表現力的解決方案。

(注意:我寫了Underscore.cfc)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM