【问题标题】:Using dynamic variables with reReplaceNoCase in a cfloop在 cfloop 中使用带有 reReplaceNoCase 的动态变量
【发布时间】:2020-09-03 13:15:52
【问题描述】:

我正在使用 cfloop 将动态表单值插入到会话结构中。我使用 reReplaceNoCase 来清理输入并且在尝试在 reReplaceNoCase 方法中使用动态表单字段名称时遇到了问题。我尝试了不同的命名方案,但无法确定语法。在发布的代码中,FORM.RTchoice[r] 失败并显示错误: Element RTCHOICE is undefined in a Java object of type class [Ljava.lang.String;.. 如何让表单字段递增“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>

【问题讨论】:

  • 那是因为 form.rtChoice 是一个字符串。所有表单变量都是简单值。为什么你认为它应该是一个结构体?
  • 我从“动态表单字段名称”中假设 OP 意味着有名为 RTchoice1RTchoice2 等的表单字段。

标签: struct coldfusion cfloop


【解决方案1】:

你可以使用

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

或者

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

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

【讨论】:

  • 与问题没有直接关系,但是..除非有特定原因使用StructInsert(),否则使用结构符号imo更简单,即&lt;cfset someStruct["dynamicKeyName"] = ....&gt;
  • 你说得很好@SOS。我已经相应地改变了我的语法。
  • 好点。如果是因为我想要allowOverwrite=false一起抛出异常,我只会在这种情况下使用StructInsert()
  • 最好添加一个示例,因此未来的读者不会认为您必须将 StructInsert 用于动态变量 :-)
猜你喜欢
  • 1970-01-01
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 2023-03-24
  • 2012-03-04
相关资源
最近更新 更多