2012-10-30 128 views
0

我有复选框动态生成,当复选框被选中onchange方法工作正常,我的功能也被调用时,该框没有被用户选中onchange方法调用我的预期功能问题是当选中该框时,页面的一部分变为活动状态,这很好,但是当用户取消选中该复选框时,该页面的该部分再次变为活动状态,并且复选框“checked”的值在第一次后保持为yes被检查。有没有办法检测用户何时选择取消选中,以便页面的活动部分变为非活动状态。复选框使用onchange

她是我的代码

echo '<table width="85%" border="1" cellpadding="0"; cellspacing="0" align="center"> 

     <tr>   
      <th width="23%" height="44" scope="col" align="left"> '.$query_rows['categoryTopic'].' <br><br><br></th> 
      <th width="24%" scope="col">'.$Count.'</th> 
      <th width="25%" scope="col"> 0 </th> 
      <th width="28%" scope="col"> <form name = "choose" method="post" action="activateImages.php"> 
      <input type="checkbox" value= "5" onChange="window.edit()" </form></th> 
     </tr> 
    </table>'; 
    } 
    ?> 

Java代码:

<script type="text/jscript"> 
//this funciton will be called when user checks a check box. 
function edit(){ 


    //get selected category from the form 
    //var formName = 'choose'; 
    //var Category = document[formName]['choose'].value; 


    //check if browser suports ajax 
    var xmlhttp = null;  
    if(typeof XMLHttpRequest != 'udefined'){ 
     xmlhttp = new XMLHttpRequest(); 
    } 

    else if(typeof ActiveXObject != 'undefined'){ 
     xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 
    } 

    else 
     throw new Error('You browser doesn\'t support ajax'); 


    //open connection with activateImages.php to recieve the active images as an acho 
    xmlhttp.open("GET", "activateImages.php",true);   

    //echeck if ready to recieve   
    xmlhttp.onreadystatechange = function(){ 

    if(xmlhttp.readyState == 4) 
     window.activate(xmlhttp); 
    }; 

    xmlhttp.send(null); 
    } 

    //recieve the active images then insert them in the specified location of the page. 
    function activate(xhr){ 

     if(xhr.status == 200){ 
      document.getElementById('images').innerHTML = xhr.responseText; 

     } 
     else 
      throw new Error('Server has encountered an error\n'+ 
      'Error code = '+xhr.status); 

} 

</script> 
+2

'='!='=='... –

+0

您应该发布代码和伪代码组合的代码(包括JavaScript),因为有太多的错误,所以几乎不可能说问题是什么。 – jeroen

+0

我们需要查看您的javascript函数edit(),以便能够帮助您充分发挥我们的能力。 – SamHuckaby

回答

0

未核对的复选框,当窗体发布不会被发送到服务器。所以,除非你明确地发出在JavaScript空值,你不应该检查的价值,而是它是否被设置:

if (isset($_POST['checkbox'])) { 

除此之外,还有在这种混合的代码和伪代码的几个误区,像未关闭的html标记,缺少复选框的名称属性,赋值而不是比较等。

编辑:它似乎你实际上没有发送任何东西到服务器,你正在混合GET(javascript )POST(删除的PHP代码)。

+0

if(isset($ _ POST ['checkbox']))它不输入它。即使在检查框时也不会设置 – user1785939

+0

删除哪个部分 – user1785939