0

根据mysql数据库中的值,我希望选中或取消选中复选框。尽管ajax查询获得了正确的值,但复选框始终保持选中状态

Jquery

<script type="text/javascript">
        $(document).ready(function() {
            $.ajax({
                type: "POST",
                url: "ajax/get_db_settings.php",
                dataType: "json",
                data: 'user='+1,
                success:function(response){
                    var taken_by_on = response.taken_by;
                    alert(taken_by_on);
                    if(taken_by_on = 0){
                        $('.taken_by_input').attr('disabled',true).css('color','#F0F0F0');
                        $('#taken_by_enable').prop('checked', false);
                    }else if(taken_by_on = 1){
                        $('.taken_by_input').attr('disabled',false).css('color','#000');
                        $('#taken_by_enable').prop('checked', true);
                    }
                },error:function (xhr, ajaxOptions, thrownError){
                alert(thrownError);
             }
         }); 
    });
</script>

get_db_settings.php

$user_id = $_POST['user'];   
$sql = "SELECT * FROM user WHERE userID = $user_id";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
if($result){
   $row = mysqli_fetch_array($result); 
   $taken_by_on = $row['takenByOn'];

   $output_array = array(
        'taken_by' => $taken_by_on
    );
}
echo json_encode($output_array);

html

<form name="add_positioning" class="postable" id="add_positioning">
<table border="0" class="autoTable_pos">
<tr>
  <td colspan="2" style="text-align:left">Enable <input style="width:10px" 
   type="checkbox" name="taken_by" id="taken_by_enable">
  </td>
</tr>
<?
$sql= "SELECT * FROM gradeReason WHERE reason_userID = $id AND category = 'positioning' AND current = 1 ORDER BY reasonID";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
while($row = mysqli_fetch_array($result)){?>
<tr>
  <td>
  <input type="text" class="taken_by_input outline ie7_td"
   name="reason[][<? echo $row['reasonID']; ?>]" value="<? echo $row['reason_name']; ?>"/>
  </td>
  <td><input type="button" name="delete_pos" value="" 
  class="taken_by_input delRow_pos del_reason_btn del_taken_in"
  onClick="UpdateRecord(<? echo $row['reasonID']; ?>);"/>
  </td>
</tr>
<?
}
?>
</table>
</form>

计划是,如果数据库中的值为 1,则选中复选框并启用表中的输入,而将禁用它们,如果值为 0,则复选框将保持未选中状态。

我正在使用anarrayAjax

4

1 回答 1

1

在 if 条件中,需要使用==not ==是赋值运算符,需要使用 equals 运算符,即==

if (taken_by_on == 0) {
    $('.taken_by_input').attr('disabled', true).css('color', '#F0F0F0');
    $('#taken_by_enable').prop('checked', false);
} else if (taken_by_on == 1) {
    $('.taken_by_input').attr('disabled', false).css('color', '#000');
    $('#taken_by_enable').prop('checked', true);
}
于 2013-11-05T16:04:58.023 回答