在cfloop中将动态变量与reReplaceNoCase一起使用

问题描述

我正在使用cfloop将动态表单值插入会话结构中。我使用reReplaceNoCase清理输入,并且在尝试在reReplaceNoCase方法中使用动态表单字段名称时遇到问题。我尝试了不同的命名方案,但无法确定语法。在发布的代码中,FORM.RTchoice [r]失败并显示错误:类型类[Ljava.lang.String; ..的Java对象中未定义元素RTCHOICE。如何获取要递增的表单字段“ r”正确吗?

<cfloop index="r" from="1" to="#APPLICATION.theCount#">
   <cfset a = StructInsert(SESSION.USER_OBJECT,"RTchoice#r#","#reReplaceNoCase(FORM.RTchoice[r],"[^a-zA-Z0-9.,(\s)-]","","all")#",1)>
    </cfloop>

解决方法

您可以使用

<cfset a = StructInsert(SESSION.USER_OBJECT,"RTchoice#r#",reReplaceNoCase(FORM['RTchoice#r#'],"[^a-zA-Z0-9.,(\s)-]","","all"),1)>

<cfset a = StructInsert(SESSION.USER_OBJECT,reReplaceNoCase(FORM['RTchoice'&r],1)>

例如:https://cffiddle.org/app/file?filepath=cafebd5c-f4b5-4fc7-93bf-ff81ca97c234/00f6a79c-7f5f-42b2-b567-8a8a371fa8aa/3c7d3229-f65f-4afe-8538-306d98adf25f.cfm