我有以下脚本应该根据从下拉列表中选择的用户名来检查和取消选中以下html表单:
<div>
<select name="user_name" id="user_name" class="user_name form-control">
<option>Please select : </option>
<?php foreach ($system_users as $value) {
?>
<option value="<?php echo $value['id']; ?>"><?php echo $value['user_name']; ?></option>
<?php
}
?>
</select>
</div>
<?php foreach ($reports_functions as $value) {
?>
<input type="checkbox" name="functions[]" id="functions" value="<?php echo $value['id']; ?>" class="form-control functions reports_functions" /> <?php echo $value['name']; ?>
<hr>
<br />
<?php
}
?>
以下是我的jquery脚本:
$("#user_name").change(function () {
var user_id = this.value;
$.ajax({
type: "GET",
url: "<?php echo base_url(); ?>admin/get_user_permissions/" + user_id,
dataType: "JSON",
success: function (response) {
$("input:checkbox").attr("checked", false);
for (i = 0; i < response.length; i++) {
var value = response[i].function_id;
$("input:checkbox[value=" + value + "]").attr("checked", true);
}
}
});
});
我遇到的问题是,当我选择用户名时,由于行$("input:checkbox").attr("checked", false);
而没有选中该复选框,但是当我删除它时,它会检查它们但不会删除之前检查的复选框。我该如何解决这个问题?