jeudi 22 octobre 2015

Jquery Checkbox Check and Uncheck not working well

I have the following script that is supposed to check and uncheck the following html form based on the user name that has been picked from the dropdown :

<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
}
?>

The following is my jquery script :

$("#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);
            }
        }
    });
});

The problem I'm having is when I select a username , the check box are not checked because of the line : $("input:checkbox").attr("checked", false); but when I remove it , it checks them but does not remove the previously checked checkboxes. How can I solve this issue?




Aucun commentaire:

Enregistrer un commentaire