如何连接两个字符串并将结果用作coldfusion中的变量名?

d.l*_*a38 7 concatenation evaluate cfml

我有一个表格,其格式有很多字段

  • 名称= "字段-1"
  • 名称= "字段-2"
  • 名称= "字段-3"
  • 名称= "字段-4"
  • 等等....

在表单操作页面上,我希望能够使用循环并能够使用循环的索引来连接像这样的字符串前缀<cfset newField = "field-" & #index#>,然后使用它#Variables.newField#来访问上一页上的表单字段.

我一直在玩这个Evaluate()功能,但没有运气.我不太多使用ColdFusion,所以我可能只是稍微偏离语法.

我如何使用它的一个例子是:

<cfset newField = "form.field-" & #index#>
<input type="hidden" 
      name="field-<cfoutput>#index#</cfoutput>" 
      value="<cfoutput>Evaluate(Variables.newField)</cfoutput>">
Run Code Online (Sandbox Code Playgroud)

Joe*_*e C 9

在这种情况下,您根本不必使用评估.只需按键名访问变量struct即可.

<cfset newField = "form.field-" & index>
<cfset value = variables[newField]>
Run Code Online (Sandbox Code Playgroud)

要不就

<cfset value = variables["form.field-#index#"]>
Run Code Online (Sandbox Code Playgroud)

或者如果您不想使用中间变量

<cfoutput>#variables["form.field-" & index]#</cfoutput>
Run Code Online (Sandbox Code Playgroud)