1

我的代码如下。我需要在页面加载时默认选中两个复选框。这将显示查询的结果。现在,当取消选中其中一个复选框时,需要提交表单并显示不同的查询结果。即使我取消选中复选框,也始终选中复选框。有人可以在这里指导我吗?

<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> 
            &nbsp;&nbsp;<input type="hidden" name="chk" id="chk1">
            <input type="checkbox" checked="checked" name="chkbox" id="chkbox2"> <strong>Active Employees</strong> 
            &nbsp;&nbsp;<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>
4

3 回答 3

5

您已经将复选框命名为相同的东西并且总是检查它们,那么为什么不检查它们呢?

您需要对它们进行唯一命名,并在提交页面后检查表单中是否存在密钥。或在表单尚未提交时将框显示为已选中

表格尚未提交 -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> 
    &nbsp;&nbsp;<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> 
   &nbsp;&nbsp;<input type="hidden" name="chk" id="chk2">
  </td>
于 2013-04-02T17:43:28.303 回答
1

有多种方法可以完成您尝试做的事情。我不确定你隐藏字段的目的是什么,所以我修改了一些东西,试图让生活更轻松一些。

有些人可能会建议 structkeyexists,但我不想介绍您可能不熟悉的新命令。

<cfparam name="form.chkbox" default="">

<form action="abc.cfm?show=yes" method="post" name="myform">
  <table align="center">
    <tr>
      <td><input type="checkbox" <cfif form.chkbox eq "" or listfind(form.chkbox, 1)>checked="checked"</cfif> name="chkbox" id="chkbox1" value="1">
        <strong> Agreement                        Only</strong> &nbsp;&nbsp;
          <input type="checkbox" <cfif form.chkbox eq "" or listfind(form.chkbox, 2)>checked="checked"</cfif> name="chkbox" id="chkbox1" value="2">
        <strong>Active                  Employees</strong> &nbsp;&nbsp;
</td>
      <td><input type="Submit" name="submitnow" value="View now"></td>
  </table>
</form>
<cfif listfind(form.chkbox, 1) and listfind(form.chkbox,2)>
  query 1
  query 2
</cfif>
于 2013-04-02T17:51:55.347 回答
0

问题是提交表单时输入(chk1chk2)确实存在于表单范围内。然而,它们的值是空的。

为了证明这一点,请在检查form之前转储范围。isdefined

<cfdump var="#form#" label="form scope">

形成范围转储

您需要检查输入的valueorlength属性。

<cfif len(form.chk1)>
    query 1
</cfif>

但!

这实际上似乎不是您想要做的。chk1并且chk2是您的TEXT输入,而不是您的CHECKBOX输入。

如果您想根据复选框进行操作,则需要检查value复选框输入的 - 这给我们带来了另一个问题:您没有value为复选框设置属性。

<input type="checkbox" value="1"...>

现在,您需要param这些复选框的值以确保字段完全存在

<cfparam name="form.chkbox1" value="0">

然后检查值

<cfif form.chkbox1 EQ 1>
    query 1
</cfif>
于 2013-04-02T18:24:09.320 回答