在coldfusion中加载页面上的复选框

时间:2013-04-10 15:12:28

标签: coldfusion

我在coldfusion表格上有2个如下所示的复选框。在页面加载时,我希望两者都被检查 并根据选中的复选框显示查询结果。我有以下代码工作正常,除了scernario我取消选中复选框和现在点击视图。正如我设置

form.chkbox="" 

在页面加载时,即使取消选中它们,它们仍会保持检查状态。如何在页面加载时保持未选中状态。我尝试使用JS函数,但它似乎没有工作

     <script type="text/javascript">
  function callme(){
var box1 = document.getElementById('chkbox1').checked;
var box2 = document.getElementById('chkbox2').checked;
if (box1 && box2){
          alert("checked") ;
}else{    
box1.checked = false;
box2.checked = false;
}
}

 </script>  

    <cfset form.chkbox=""> 

    <form action="view_emp_qual.cfm?show=yes" method="post" name="Myform">
     <table align="center">
     <tr>
    <td>
  <cfif isDefined("form.chkbox") and (form.chkbox eq "" or listfind(form.chkbox, 1))>
  <input type="checkbox" checked="checked" name="chkbox" id="chkbox1" value="1">      
  <cfelse>
  <input type="checkbox" name="chkbox"  id="chkbox1" value="1">
   <input type="hidden" name="chkbox"  id="chkbox1" value="1">
  </cfif>
    <strong> Agreement Only</strong> &nbsp;&nbsp;
    <cfif isDefineD("form.chkbox") and (form.chkbox eq "" or listfind(form.chkbox, 2))>
      <input type="checkbox" checked="checked" name="chkbox" id="chkbox2" value="2">
    <cfelse>
     <input type="checkbox" name="chkbox"  id="chkbox2" value="2">
      <input type="hidden" name="chkbox"  id="chkbox2" value="2">
    </cfif>

    <strong>Active Employees</strong> &nbsp;&nbsp;
   </td>
  <td><input type="Submit" name="submitnow" value="View Selected" class="button"  onclick="return callme();">      </td>

      </tr>
      </table>
      </form> 


    <cfif not isDefined("form.chkbox")>
     Query1
     <cfelseif isDefined("form.chkbox") and ( listfind(form.chkbox, 1) eq 0 and listfind(form.chkbox, 2) eq 1)>
      Query 2
     <cfelseif isDefineD("form.chkbox") and (listfind(form.chkbox, 1) eq 1 and listfind(form.chkbox, 2) eq 0)>
    query 3
     <cfelse>
    query4
    </cfif>

1 个答案:

答案 0 :(得分:0)

如果您在同一页面提交表格,则以下代码可能会解决您的目的。

我已经使用您的主要代码进行了一些调整。请看看。

    <cfdump var="#form#" label="before">
    <!---flag for checking if form has submitted or not--->
    <cfif Not isDefined("form.submitnow")>
        <cfset form.chkbox = "1,2">
    </cfif>
    <cfdump var="#form#" label="after">
    <cfif isDefined("form.chkbox") > 
    <cfdump var="#listfind(form.chkbox, 1)#" ><br>
    <cfdump var="#listfind(form.chkbox, 2)#" >
    </cfif>


        <form action="" method="post" name="Myform">
            <table align="center">
                <tr>
                    <td>
                        <!---I have removed hidden fields and made a few changes in conditional checking    --->
                        <cfif isDefined("form.chkbox") and (listfind(form.chkbox, 1))> 
                            <input type="checkbox" checked="checked" name="chkbox" id="chkbox1" value="1"><span>1</span>      
                        <cfelse>
                            <input type="checkbox" name="chkbox"  id="chkbox1" value="1"><span>2</span> 
                        </cfif>

                        <strong> Agreement Only</strong> &nbsp;&nbsp;
                        <cfif isDefineD("form.chkbox") and (listfind(form.chkbox, 2))>
                            <input type="checkbox" checked="checked" name="chkbox" id="chkbox2" value="2"><span>3</span> 
                        <cfelse>
                            <input type="checkbox" name="chkbox"  id="chkbox2" value="2"><span>4</span> 
                        </cfif>
                        <strong>Active Employees</strong> &nbsp;&nbsp;
                    </td>
                    <td>
                        <input type="Submit" name="submitnow" value="View Selected" class="button">      
                    </td>

                </tr>
            </table>
          </form> 


        <cfif not isDefined("form.chkbox")>
         Query1
         <cfelseif isDefined("form.chkbox") and ( listfind(form.chkbox, 1) eq 0 and listfind(form.chkbox, 2) eq 1)>
          Query 2
         <cfelseif isDefineD("form.chkbox") and (listfind(form.chkbox, 1) eq 1 and listfind(form.chkbox, 2) eq 0)>
        query 3
     <cfelse>
    query4
    </cfif>