我的代码如下。我需要在页面加载时默认选中这两个复选框。这将显示查询结果。现在当取消选中其中一个复选框时,需要提交表单并需要显示不同的查询结果。即使我取消选中复选框,复选框也始终处于选中状态。有人可以在这里指导我吗?
<form action="abc.cfm?show=yes" method="post" name="myform">
<table align="center">
<tr>
<td>
<input type="checkbox" checked="checked" name="chkbox" id="chkbox1"> <strong> Agreement Only</strong>
<input type="hidden" name="chk" id="chk1">
<input type="checkbox" checked="checked" name="chkbox" id="chkbox2"> <strong>Active Employees</strong>
<input type="hidden" name="chk" id="chk2">
</td>
<td>
<input type="Submit" name="submitnow" value="View now">
</td>
</table>
</form>
<cfif isdefined("form.chk1")>
query 1
<cfelseif isdefined("form.chk2")>
query 2
</cfif>
最佳答案
您已将复选框命名为相同的名称并且始终选中它们,那么为什么不选中它们呢?
您需要对它们进行唯一命名,并在页面提交后检查该键是否存在于表单中。或者在表单未提交时显示复选框为选中状态
表单尚未提交 - NOT structKeyExists(form,'fieldnames')
表单已提交并选择了 chkbox1 - structKeyExists(form,'chkbox1')
<td>
<input type="checkbox"<cfif NOT structKeyExists(form,'fieldnames') OR structKeyExists(form,'chkbox1')> checked="checked"</cfif> name="chkbox1" id="chkbox1"> <strong> Agreement Only</strong>
<input type="hidden" name="chk" id="chk1">
<input type="checkbox"<cfif NOT structKeyExists(form,'fieldnames') OR structKeyExists(form,'chkbox2')> checked="checked"</cfif> name="chkbox2" id="chkbox2"> <strong>Active Employees</strong>
<input type="hidden" name="chk" id="chk2">
</td>
关于javascript - ColdFusion 表单中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15770423/