dotxxh0998 2013-12-31 05:07
浏览 50
已采纳

Javascript onChange checkBox

Ok guys so I am hopelessly new to Javascript. I have been doing good so far. But I can't seem to figure out what the issue is here. I am trying to send some data to a php file via GET to change some data on the database when you check the checkbox. Here's the code.

<?
$comple = $tasking['status'];
if($comple=="complete"){
?>
<input type="checkbox" name="<? echo $tasking['id']; ?>" checked="true"
onchange="change()"> <? echo $tasking['detail']; ?><br>
<?
}
else{
?>
<input type="checkbox" name="<? echo $tasking['id']; ?>" onchange="change()"> <? echo 
$tasking['detail']; ?><br>
<?
}
?>



<script>
function change(){
var id = document.getElementById(this).name;
var ch = document.getElementById(this).checked;
var sendto = 'changetask.php?id=' + id + '&&ch=' + ch;
if(window.XMLHttpRequest){
  xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET",sendto,false);
  xmlhttp.send(null);
}
else{
  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  xmlhttp.open("GET",sendto,false);
  xmlhttp.send();
}

}
</script>
  • 写回答

2条回答 默认 最新

  • douding6266 2013-12-31 05:31
    关注

    Try this

    Demo : http://jsbin.com/obiFisIL/1/edit

    ur html code should be

    <input type="checkbox" name="<? echo $tasking['id']; ?>" checked="true"
    onchange="change(this)"> <? echo $tasking['detail']; ?>
    

    In ur html I added "this" in onchange function as parameter.....

    ur js should be

    function change(dis){
    var id = dis.name;
    var ch = dis.checked;
    -----
    

    Full code

    <?
    $comple = $tasking['status'];
    if($comple=="complete"){
    ?>
    <input type="checkbox" name="<? echo $tasking['id']; ?>" checked="true"
    onchange="change(this)"> <? echo $tasking['detail']; ?><br>
    <?
    }
    else{
    ?>
    <input type="checkbox" name="<? echo $tasking['id']; ?>" onchange="change(this)"> <? echo 
    $tasking['detail']; ?><br>
    <?
    }
    ?>
    
    
    
    <script>
    function change(dis){
    var id = dis.name;
    var ch = dis.checked;
    var sendto = 'changetask.php?id=' + id + '&&ch=' + ch;
    if(window.XMLHttpRequest){
      xmlhttp = new XMLHttpRequest();
      xmlhttp.open("GET",sendto,false);
      xmlhttp.send(null);
    }
    else{
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      xmlhttp.open("GET",sendto,false);
      xmlhttp.send();
    }
    
    }
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况