未选中单选按钮会中断页面,但如果选中则有效



我有一个简单的表单,我正在尝试将数据传递给该表单,该表单在操作页面上有一封电子邮件,该电子邮件将我放入表单中的数据发送到该表单中。

在我的表单页面上。我有一个文本字段和两个单选按钮。

如果我不将数据放入其中,我的文本字段就会起作用。但是,在我的单选按钮上,如果我检查它,页面可以工作,但如果我根本不检查它,则不起作用,这会导致我的分页,我不确定我做错了什么?如果我没有选中任何一个单选按钮,我希望页面处理默认值为"否"。我必须验证什么的吗?

这是我的代码。

<cfparam name="form.firstName" default="">
<cfparam name = "form.optradio1" default="no">
<form action="test.cfm" method="post">
<label for="firstName"></label>
<input type="text" name="firstName">
<input type="radio" name="optradio1" Value="Male" <cfif form.optradio1 eq "Yes">checked</cfif>>
</form>

这就是单选和复选框输入在 HTML 中的工作方式。如果未选中,则不会在提交表单中提交。

要确定是否检查了无线电输入,您可以使用structKeyExists(form, <name of the input, as string>)
structKeyExists(form, "optradio1").

<cfparam name="form.firstName" default="">
<form action="test.cfm" method="post">
<label for="firstName"></label>
<input type="text" name="firstName">
<input type="radio" name="optradio1" Value="Male" <cfif structKeyExists(form, "optradio1")>checked</cfif>>
</form>

假设您有两个无线电输入:

<cfparam name="form.firstName" default="">
<form action="test.cfm" method="post">
<label for="firstName"></label>
<input type="text" name="firstName">
<input type="radio" name="optradio1" Value="Male" <cfif structKeyExists(form, "optradio1") and form.optradio1 eq "Male">checked</cfif>>
<input type="radio" name="optradio1" Value="Female" <cfif structKeyExists(form, "optradio1") and form.optradio1 eq "Female">checked</cfif>>
</form>

您的初始代码不起作用,因为:

  • 如果选中,则form.optradio1等于Male
  • 如果未选中,则由于<cfparam name = "form.optradio1" default="no">form.optradio1
    默认为no

相关内容