我正在尝试使用会话变量创建表单。我可以链接回表单并显示用户之前输入的内容。我找到了一种方法来显示输入框值,如下所示: 的 INPUT
<cfif structKeyExists(session, 'firstName')>
<cfset form_first_name = "#session.firstName#">
</cfif>
</cfif>
<input id="firstName" name="firstName" type="text" value="<cfif isDefined("form_first_name")><cfoutput>#form_first_name#</cfoutput></cfif>" />
但我正在努力为单选按钮和下拉菜单做同样的事情。如下:
单选
<cfif isDefined("session")>
<cfif structKeyExists(session, 'gender')>
<cfset form_gender = "#session.gender#">
</cfif>
</cfif>
<input id="gender" name="gender" type="radio" value="<cfif isDefined("form_gender")><cfoutput>#form_gender#</cfoutput></cfif>" />Male
<input id="gender" name="gender" type="radio" value="<cfif isDefined("form_gender")><cfoutput>#form_gender#</cfoutput></cfif>" />Female
DROPDOWN
<cfif isDefined("session")>
<cfif structKeyExists(session, 'vehiclebody')>
<cfset form_vehiclebody = "#session.vehiclebody#">
</cfif>
</cfif>
<select name="vehiclebody" id="vehiclebody" required>
<option value="">Select Body</option>
<option value="2D">2-Door Coupe</option>
<option value="3D">3-Door Hatchback</option>
<option value="4D">4-Door Sedan</option>
<option value="5D">5-Door Hatchback</option>
</select>
非常感谢任何帮助!
答案 0 :(得分:1)
您需要的是:
<input
id="gender"
name="gender"
type="radio"
value="Male"
<cfif isDefined("form_gender") AND form_gender EQ "Male" >checked</cfif>
/>Male
注意:您更改了示例中的value
字段。但您不想这样做:标记为“男性”的字段的值应为“男性”,标记为“女性”的字段的值应标记为“女性”。否则,用户选择可能与标签不匹配。
您需要的是:
<option
value="2D"
<cfif isDefined("session.vehiclebody") AND session.vehiclebody EQ "2D">selected</cfif>
>2-Door Coupe</option>
答案 1 :(得分:1)
你会做这样的事情:
<cfif structKeyExists(session, "vehiclebody")>
<cfset form_vehiclebody = session.vehiclebody>
<cfelse>
<cfset form_vehiclebody = "">
</cfif>
<cfif structKeyExists(session, "gender")>
<cfset form_gender = session.gender>
<cfelse>
<cfset form_gender = "">
</cfif>
<!--- start your form... --->
<input id="gender" name="gender" type="radio" value="Male" <cfif form_gender eq "Male">checked</cfif>>Male
<input id="gender" name="gender" type="radio" value="Female" <cfif form_gender eq "Female">checked</cfif>>Female
<select name="vehiclebody" id="vehiclebody" required>
<option value="" <cfif form_vehiclebody eq "">selected</cfif>>Select Body</option>
<option value="2D" <cfif form_vehiclebody eq "2D">selected</cfif>>2-Door Coupe</option>
<option value="3D" <cfif form_vehiclebody eq "3D">selected</cfif>>3-Door Hatchback</option>
<option value="4D" <cfif form_vehiclebody eq "4D">selected</cfif>>4-Door Sedan</option>
<option value="5D" <cfif form_vehiclebody eq "5D">selected</cfif>>5-Door Hatchback</option>
</select>