简体   繁体   English

取消选中复选框时AJAX不起作用

[英]AJAX not working when checkbox is unchecked

I am trying to changed the value of my flag on db everytime I check or unchecked the checkbox. 每当我选中或取消选中复选框时,我都试图更改db上的标志值。 But for some reason, it ajax is only firing when im changing my checkbox from checked -> unchecked . 但是由于某种原因,它只会在我将我的复选框从checked > unchecked更改时触发。

HTML HTML

    <table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Lot ID                  </th>
                <th>Lot Name                </th>
                <th>Block Located         </th>
                <th>Status      </th>
                <th>Action      </th>
            </tr>
        </thead>
        <tbody>
    <?php
    $sql = $db->prepare("SELECT * from tbl_lot
                        LEFT JOIN tbl_block ON tbl_lot.blockID = tbl_block.blockID
                        WHERE lotStatus <> 2");

    $sql->execute();
        while($result = $sql->fetch(PDO::FETCH_ASSOC))
        {
          $id         = $result['lotID'];
          $lotName    = $result['lotName'];
          $status     = ($result['lotStatus']==1) ? "checked" : "";
          $blockName  = $result['blockName'];
          $blockID    = $result['blockID'];

          echo "
          <tr>
            <td>$id</td>
            <td>$lotName</td>
            <td>$blockName</td>
            <td>
            <input type='checkbox'  onchange='switchStatus($id,$status)'  data-toggle='toggle' $status>
            </td>
            <td>
            <div class='btn-group'  role='group'>
              <input type='button'  value='Manage'  onclick='Xmanage($id,$blockID,\"$lotName\")'  class='btn btn-info'>
              <input type='button'  value='Remove'  onclick='Xdelete($id)'                            class='btn btn-danger'>
            </div>
            </td>
          </tr>
          ";
        }
        ?>
        <tbody>
</table>

Here's my AJAX Code: 这是我的AJAX代码:

    function switchStatus(id,status){
  var theID = id;
  var theStatus = status;
  if(theStatus==1){
  $.ajax({
      url:    "ajax/updateProjectStatus.php",
      type:   "POST",
      data:   {
        projectID : theID,
        status    : theStatus
      },
      cache:  false,
      success: function (data){
        alert(data);
      }
  });
  }
}

updateProjectStatus.php updateProjectStatus.php

 <?php
include "../../connection/connection.php";
$id                  =  $_POST['projectID'];
$prevStats       =  $_POST['status'];
if($prevStats==1){$status = 1;}else{$status=0;}
$sql                 =  "UPDATE tbl_project  set projectStatus = '$status' WHERE projectID = '$id'";
$query             =    $db->prepare($sql);
$results           =    $query->execute();

?>

You have to change Following Code. 您必须更改以下代码。

HTML HTML

<input type='checkbox' onchange='switchStatus($id, this)' data-toggle='toggle' $status>

AJAX Code AJAX代码

function switchStatus(id,status){
    var theID = id;
    var theStatus = $(status).prop('checked');
    if(theStatus){
       theStatus = 1;
    } else {
       theStatus = 0;
    }
    $.ajax({
       url  : "ajax/updateProjectStatus.php",
       type : "POST",
       data : {
           projectID : theID,
           status    : theStatus
       },
       cache   :  false,
       success : function (data){
          alert(data);
       }
    });
}

updateProjectStatus.php updateProjectStatus.php

<?php
include "../../connection/connection.php";
$id = $_POST['projectID'];
$status = $_POST['status'];
$sql = "UPDATE tbl_project  set projectStatus = '$status' WHERE projectID = '$id'";
$query = $db->prepare($sql);
$results = $query->execute();
?>

You need to change following lines 您需要更改以下几行

 $status     = ($result['lotStatus']==1) ? "checked" : "";

to

$status     = $result['lotStatus'];
$checked_or_not="";
if($status==1){ $checked_or_not= "checked"; }

And change checkbox as this 然后更改复选框

 <input type='checkbox' id="check"  onchange='switchStatus($id)'  data-toggle='toggle'  value="$status"  $checked_or_not>

And change your script as this 并以此更改脚本

function switchStatus(id) {
    var theID = id;
    var theStatus = 0;
    if (document.getElementById("check").checked == true) {
        theStatus = 1;
    }
    $.ajax({
        url: "ajax/updateProjectStatus.php",
        type: "POST",
        data: {
            projectID: theID,
            status: theStatus
        },
        cache: false,
        success: function (data) {
            alert(data);
        }
    });
}

I think it will do what you need to do 我认为它将满足您的需求

You can check the status of checkbox whether it is checked or unchecked by using following code : 您可以使用以下代码来检查复选框的状态:选中还是未选中:

if($('#'+your_id).is(':checked') || $('#'+your_id).prop('checked')) 
  {
    // do something if checked
  }
  else
  {
    // do something if unchecked
  }

Happy Coding :) 快乐编码 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM