通过CFLOOP动态设置radiobutton状态

时间:2012-10-16 20:57:17

标签: coldfusion cfloop

我正在使用cfloop来动态创建/填充三组输入字段,以及一些用于评级系统的单选按钮。输入字段按预期工作。但是我遇到了单选按钮的问题。

出于某种原因(我假设这是一个简单的原因)单选按钮不会反映发送给它们的值。例如:假设我有3件事我评价。如果循环值为5,4,3,则单选按钮全部显示为传递给它们的值为5。这几乎就好像所有的字段集都是先创建的并且都取第一个值(例如得到检查),而不是创建第一个字段集,插入值,然后创建第二个字段集等等(就像我一样)会假设在循环中)。同样,它适用于除了单选按钮之外的所有内容。任何见解将不胜感激。

这是我的代码:

<cfloop query="postedBy" startrow="1" endrow="4">
    <cfquery name="score" datasource="myDB">
    SELECT Round(sum(leadership)/Count(leadership)) as leadership
           ,  Round(sum(communication)/Count(communication)) as communication
           ,  Round(sum(fairness)/Count(fairness)) as fairness
           , Round(sum(ethics)/Count(ethics)) as ethics
           , Round(sum(competence)/Count(competence)) as competence 
    FROM  score_base 
    WHERE score_ID = '#postedBy.score_id#'
    </cfquery>

    <cfset score_ID=#postedBy.score_id#>
    <cfoutput>id: #postedBy.score_id#</cfoutput>
    <cfoutput>My score: #score.leadership#, #score.communication#, #score.fairness#, #score.ethics#, #score.competence#</cfoutput>

    <cfset counter = counter+1>
    <cfset "currentScore#score.leadership#" = "checked">
    <cfset "currentScoreb#score.communication#" = "checked">
    <cfset "currentScorec#score.fairness#" = "checked">
    <cfset "currentScored#score.ethics#" = "checked">
    <cfset "currentScoree#score.competence#" = "checked">   

     ...
    <div class="rating-wrapper">
        <cfform>
        <label>Leadership</label>
        <cfinput type="radio" class="star" name="leadership" value="1" disabled="disabled" checked = '#currentScore1#'/>
        <cfinput type="radio" class="star" name="leadership" value="2" disabled="disabled" checked = '#currentScore2#'/>
        <cfinput type="radio" class="star" name="leadership" value="3" disabled="disabled" checked = '#currentScore3#'/>
        <cfinput type="radio" class="star" name="leadership" value="4" disabled="disabled" checked = '#currentScore4#'/>
        <cfinput type="radio" class="star" name="leadership" value="4" disabled="disabled" checked = '#currentScore5#'/>
        </cfform>
   </div><!--    END div class="rating-wrapper"    -->
   <div class="rating-wrapper">
       <cfform>
           <label>Communication</label>
           <cfinput type="radio" class="star" name="communication" value="1" disabled="disabled" checked = '#currentScoreb1#'/>
           <cfinput type="radio" class="star" name="communication" value="2" disabled="disabled" checked = '#currentScoreb2#'/>
           <cfinput type="radio" class="star" name="communication" value="3" disabled="disabled" checked = '#currentScoreb3#'/>
           <cfinput type="radio" class="star" name="communication" value="4" disabled="disabled" checked = '#currentScoreb4#'/>
           <cfinput type="radio" class="star" name="communication" value="5" disabled="disabled" checked = '#currentScoreb5#'/>
      </cfform>
   </div><!--    END div class="rating-wrapper"    -->
   <div class="rating-wrapper">
       <cfform>
        <label>Fairness</label>
        <cfinput type="radio" class="star" name="fairness" value="1" disabled="disabled" checked = '#currentScorec1#'/>
        <cfinput type="radio" class="star" name="fairness" value="2" disabled="disabled" checked = '#currentScorec2#'/>
        <cfinput type="radio" class="star" name="fairness" value="3" disabled="disabled" checked = '#currentScorec3#'/>
        <cfinput type="radio" class="star" name="fairness" value="4" disabled="disabled" checked = '#currentScorec4#'/>
        <cfinput type="radio" class="star" name="fairness" value="5" disabled="disabled" checked = '#currentScorec5#'/>
    </cfform>
   </div><!--    END div class="rating-wrapper"    -->
   <div class="rating-wrapper">
      <cfform>
       <label>Ethics</label>
       <cfinput type="radio" class="star" name="ethics" value="1" disabled="disabled" checked = '#currentScored1#'/>
       <cfinput type="radio" class="star" name="ethics" value="2" disabled="disabled" checked = '#currentScored2#'/>
       <cfinput type="radio" class="star" name="ethics" value="3" disabled="disabled" checked = '#currentScored3#'/>
       <cfinput type="radio" class="star" name="ethics" value="4" disabled="disabled" checked = '#currentScored4#'/>
       <cfinput type="radio" class="star" name="ethics" value="5" disabled="disabled" checked = '#currentScored5#'/>
      </cfform>
   </div><!--    END div class="rating-wrapper"    -->
....
</cfloop>

1 个答案:

答案 0 :(得分:0)

想出来:问题在于我的动态变量。我需要在循环结束时重置它们:

<cfset "currentScore#score.leadership#" = "0">
<cfset "currentScoreb#score.communication#" = "0">
<cfset "currentScorec#score.fairness#" = "0">
<cfset "currentScored#score.ethics#" = "0">
<cfset "currentScoree#score.competence#" = "0"> 
</cfloop>