2017-09-06 175 views
0

我想使用AJAX標記狀態。我成功地使用AJAX將標誌狀態改爲Active或Deactive,我的目標是在標誌狀態爲1或0時禁用按鈕,問題出現在'if子句'中。該按鈕完全「禁用」在我的桌子上。你能幫助弟弟嗎?激活/取消激活按鈕定位標記

這是我所取得的成就:

$sql= "SELECT * FROM kelas"; 
$query= mysqli_query($koneksi, $sql); 
while ($row=mysqli_fetch_array($query)) 
    { 
    $id = $row['id']; 
    $id_kelas = $row['id_kelas']; 
    $nama_kelas = $row['nama']; 
    $id_pengajar = $row['id_pengajar']; 
    $id_siswa = $row['id_siswa']; 
    $status = $row['status'];  

    echo "<tr>"; 
     echo "<td><label class=\"control-label\">$nama_kelas</label></td>"; 
     echo "<td> 
       <a href=\"javascript:void(0)\" rel=\"{$id}\" class=\"btn btn-primary aktifkankelas\" if $status == '1' { echo disabled }>Aktifkan</a> 
       <a href=\"javascript:void(0)\" rel=\"{$id}\" class=\"btn btn-primary nonaktifkankelas\" if $status == '0' { echo disabled }>Nonaktifkan</a> 
       </td>"; 
    echo "</tr>"; 
    } 

的AJAX

$(".aktifkankelas").click(function(e) { 
    e.preventDefault(); 
    var id = $(this).attr('rel'); 
    $.ajax({ 
     url: "../admin-app/admin-update-aktifkankelas-app.php", 
     type: "POST", 
     crossDomain: true, 
     data: 'updateaktif='+id, 
     dataType: 'json', 
    }); 
}); 

$(".nonaktifkankelas").click(function(e) { 
    e.preventDefault(); 
    var id = $(this).attr('rel'); 
    $.ajax({ 
     url: "../admin-app/admin-update-nonaktifkankelas-app.php", 
     type: "POST", 
     crossDomain: true, 
     data: 'updatenonaktif='+id, 
     dataType: 'json', 
    }); 
}); 
+0

的回聲?不會工作... –

+0

我已經省略了回聲,它仍然不起作用 – mending3

回答

0

使用這樣的,不傳遞echo呼應。

if($status){ 
    $aktifkan_status = "disabled"; 
    $nonaktifkan_status = ""; 
}else{ 
    $aktifkan_status = ""; 
    $nonaktifkan_status = "disabled"; 
} 

echo "<tr>"; 
echo "<td><label class=\"control-label\">$nama_kelas</label></td>"; 
echo "<td> 
     <a id='aktifkankelas' href='javascript:void(0)' rel='{$id}' class='btn btn-primary aktifkankelas' $aktifkan_status>Aktifkan</a> 
     <a id='nonaktifkankelas' href='javascript:void(0)' rel='{$id}' class='btn btn-primary nonaktifkankelas' $nonaktifkan_status>Nonaktifkan</a> 
     </td>"; 
echo "</tr>"; 

如果阿賈克斯後,需要你應該在阿賈克斯

dataType: 'json', 
}).done(function() { 
    $("#id_of_anchor").attr("disabled", true);// pass id of anchor to disabled or enable 
}); 
+0

哇,兄弟。它的作用就像一種魅力。 – mending3

-1

添加.done更多的控制只是做:回聲內

if($status == '1') 
{ 
    $status_class = 'disabled'; 
} 
else { $status_class = 'enabled'; } 
echo '<tr>'; 
     echo '<td><label class="control-label">'.$nama_kelas.'</label></td>'; 
     echo '<td> 
       <a href="javascript:void(0)" rel="'.$id.'" class="btn btn-primary aktifkankelas '.$status_class.'">Aktifkan</a> 
       </td>'; 
echo '</tr>';`