根据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,则会禁用它们并且复选框将保持未选中状态。
我正在使用array
传递Ajax
中的值,因为我打算在工作后添加更多内容
答案 0 :(得分:1)
在if条件中,您需要使用==
而非=
,=
是赋值运算符,您需要等于==
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);
}