激活/取消激活按钮锚标记

时间:2017-09-06 13:30:11

标签: php jquery mysql ajax

我想使用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',
    });
});

2 个答案:

答案 0 :(得分: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>";

如果您需要在ajax之后,则应在ajax中添加.done

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

答案 1 :(得分:-1)

要获得更多控制权,请执行以下操作:

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>';`