表单提交后保留下拉列表值

发布于 2024-11-11 14:53:23 字数 540 浏览 1 评论 0原文

我有一个表单,用户对一首诗进行从 1 到 3 的评分。我的代码如下:

<select name="rating">
<cfif len(duplicateCheck.score)><option value="#duplicateCheck.score#">You scored:  #duplicateCheck.score#</option>
<cfelse><option value="">&ndash; Rate This Poem &ndash;</option>
</cfif>
<option value="1">1</option>
<option value="2">2</option>
    <option value="3">3</option>
 </select> 

如果用户已经对这首诗进行了评分,我会尝试选择他们之前的分数。如果没有,用户可以选择1-3。我该怎么做?

I have a form where a user rates a poem from 1 to 3. My code is as follows:

<select name="rating">
<cfif len(duplicateCheck.score)><option value="#duplicateCheck.score#">You scored:  #duplicateCheck.score#</option>
<cfelse><option value="">– Rate This Poem –</option>
</cfif>
<option value="1">1</option>
<option value="2">2</option>
    <option value="3">3</option>
 </select> 

If the user has already rated the poem, I am trying to make their previous score be selected. If not, the user can select 1-3. How should I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆头 2024-11-18 14:53:23

取决于您如何存储用户已经对这首诗进行评分的事实。但从高层次来看:

<option value="1"<cfif userHasSelected eq 1> selected="selected"</cfif>>1</option>
<option value="2"<cfif userHasSelected eq 2> selected="selected"</cfif>>2</option>
<option value="3"<cfif userHasSelected eq 3> selected="selected"</cfif>>3</option>

那么,您是否已经掌握了用户是否对这首诗进行了评分?或者这是真正的问题吗?

Depends on how you're storing the fact that the user has already rated the poem. But from a high level:

<option value="1"<cfif userHasSelected eq 1> selected="selected"</cfif>>1</option>
<option value="2"<cfif userHasSelected eq 2> selected="selected"</cfif>>2</option>
<option value="3"<cfif userHasSelected eq 3> selected="selected"</cfif>>3</option>

So, do you already have a handle on whether or not the user has rated the poem? Or is that the actual question?

べ映画 2024-11-18 14:53:23

如果您循环浏览选项列表,您可以动态地执行此操作。

<cfloop from="1" to="3" index="thisOption">
   <option value="#thisOption#" <cfif userHasSelected eq thisOption> selected="selected"   
   </cfif>>#thisOption#</option>
</cfloop>

或者您可以移动代码以从选项 html 中选择下拉列表,这是我更喜欢的。

<cfloop from="1" to="3" index="thisOption">
   <cfset variables.selected = userHasSelected eq thisOption? 'selected' : '' />
   <option value="#thisOption#" #selected#>#thisOption#</option>
</cfloop>

If you loop through your list of options you could do this dynamically.

<cfloop from="1" to="3" index="thisOption">
   <option value="#thisOption#" <cfif userHasSelected eq thisOption> selected="selected"   
   </cfif>>#thisOption#</option>
</cfloop>

Or you can move the code to select the drop down out of the option html, which i prefer.

<cfloop from="1" to="3" index="thisOption">
   <cfset variables.selected = userHasSelected eq thisOption? 'selected' : '' />
   <option value="#thisOption#" #selected#>#thisOption#</option>
</cfloop>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文