在页面上保留表格的值 [英] Keeping the values of form on page

查看:75
本文介绍了在页面上保留表格的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户提交表单后,我希望保留用户选择的值。

I want to keep the values chosen by the user on the form once they submit the form.

这是我尝试过的操作:

<cfset tx_name = "">
<cfset id_age1 = "">

<cfif isDefined("form.tx_name")>
    <cfset tx_name = form.tx_name>

    <cfif isDefined("a1")>
        <cfset id_age1 = form.a1>
    </cfif>
</cfif>

<cfoutput>

<input type="text" name="tx_name" value="#tx_name#">

<select name="id_age1">
    <cfloop from="1" to="20" index="a1">
        <option value="#a1#">#a1#</option>
    </cfloop>
</select>

</cfoutput>

我正在得到 tx_name 的预期结果,但是 id_age1 没有显示正确的结果。它只是重置回 1 。我在做什么错?

I am getting the expected result for tx_name, however, the id_age1 is not displaying the correct result. It just resets back to 1. What am I doing wrong?

推荐答案

大多数该代码都是不必要的。要为不存在的变量(不同于空变量)定义默认值,只需使用 cfparam 。请务必指定变量作用域,以避免由于作用域冲突而导致意外结果:

Most of that code unnecessary. To define default values for variables that do not exist (different than being empty), simply use cfparam. Be sure to specify the variable scope to avoid unexpected results due to scope conflicts:

    <cfparam name="form.tx_name" default="">
    <cfparam name="form.id_age1" default="">

要在< select> 列表,您必须应用 selected 属性分配给相应的< option>

To pre-select an item within the <select> list, you must apply the selected attribute to the appropriate <option>:

<select name="id_age1">
   <cfloop ...> 
      <option value="#a1#" <cfif a1 eq form.id_age1>selected</cfif>>
         #a1#
      </option>
   </cfloop>
</select>

此外,不确定是否故意将其省略,但是..两个表单字段应嵌套在内部< form> 标签。

Also, not sure if it was omitted deliberately, but .. the two form fields should be nested inside a <form> tags.

这篇关于在页面上保留表格的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆