2016-07-22 83 views
2

我試圖在每次選中或取消選中該複選框時更改我的db標誌的值。但由於某種原因,它只是當我改變我的複選框從checked - >unchecked纔開火。未選中複選框時AJAX無法正常工作

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> 

這裏是我的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

<?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(); 

?> 

回答

2

您必須更改下面的代碼。

HTML

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

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

<?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(); 
?> 
+0

表彰爲您提供:d謝謝:)只是一個 最後一個問題。 'this'具體做什麼?

+0

** this **是當前元素的對象。 –

1

您需要更改以下行

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

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

而變化選擇框,因爲這

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

,改變你的腳本,因爲這

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); 
     } 
    }); 
} 

,我認爲它會做你需要做的

1
什麼

您可以查看統計我們複選框無論是使用下面的代碼選中或取消選中:

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

編碼快樂 :)