用唯一命名的输入字段替换占位符值

时间:2018-05-18 18:04:03

标签: coldfusion

我有以下代码,我在保存的内容中有占位符。我想用输入字段替换占位符值。我能够成功地做到这一点。但是,我需要重命名所有输入复选框字段,如1,2,3,4,但不使用JSoup库,因为这有点过分。还有什么我可以尝试的吗?

<cfsavecontent variable="h">
    <cfoutput>#mytable.h#</cfoutput>
</cfsavecontent>
<cfset h= Replace(h,"[Check Box]","<input type='checkbox' name='abc' id='abc'>","all")>

以上创建了具有相同名称的复选框,我不想这样做。

1 个答案:

答案 0 :(得分:2)

这是一种丑陋而又有点蛮力的东西,但是它应该让你知道一种解决这个问题的方法。

<!--- Whatever your h value comes from --->
<cfset h = "[Check Box] Some text. <br>
    [Check Box] Some text. <br>
    [Check Box] Some text. <br>
    [Check Box] Some text. <br>
    [Check Box] Some text. <br>
    [Check Box] Some text. <br> 
    [Check Box] Some text. <br>
    [Check Box] Some text. <br>
">

<!--- 
    We replace the searched for text with a single character delimiter 
    so that we can count elements. We also append a nonsense character 
    to the string to make sure that if our searh string is the first 
    thing in the full string, it will still get replaced. 
--->
<cfset changeDelims = replace('a' & h,"[Check Box]","|","All")>
<!--- 
    Now we can count how many elements are in the main string. This 
    will tell us how many times we need to replace our substring. 
--->
<cfset howMany = listLen(changeDelims,"|")-1>

<!--- 
    Now loop through your string for the number of times we have that
    substring and replace it. 
--->
<cfloop from="1" to="#howMany#" index="i">
    <cfset replaceStr = "<input type='checkbox' name='abc#i#' id='abc#i#'>")>
    <cfset h = replace(h,"[Check Box]", replaceStr)>
</cfloop> 

<!--- Which gives us.... ---->
<cfoutput>#h#</cfoutput>