我的代码如下。页面加载时,默认情况下,我需要选中两个复选框。这将显示查询结果。现在,取消选中其中一个复选框时,需要提交表单,并且需要显示不同的查询结果。即使取消选中该复选框,也始终会选中这些复选框。有人可以在这里指导我吗?
<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>