0

動的に生成されるチェック ボックスがあります。チェック ボックスをオンにすると、onchange メソッドが正常に動作し、ユーザーがボックスをオフにすると、関数が呼び出されます。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>
4

1 に答える 1

0

チェックされていないチェックボックスは、フォームが投稿されたときにサーバーに送信されません。したがって、JavaScript で明示的に空の値を送信しない限り、値を確認するのではなく、設定されているかどうかを確認する必要があります。

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

それとは別に、このコードと疑似コードの混合には、閉じられていない html タグ、チェックボックスの name 属性の欠落、比較ではなく代入など、いくつかのエラーがあります。

編集:実際にはサーバーに何も送信しておらず、GET (javascript) POST (削除された php コード) を混在させているようです。

于 2012-10-30T16:23:38.060 に答える