I have a select dependent on another, in the first select the Department where a user works and in the second select the users that work in that department are loaded, this works well the first time a department is chosen, but if another department is chosen, it does not refresh the new users, but it keeps the ones that I charge with the first selection.
$(document).ready(function() {
$("#project_id").change(function() {
$("#project_id option:selected").each(function() {
project_id = $(this).val();
$.post("ajax/getassigned_user.php", {
project_id: project_id
}, function(data) {
$("#user_id").html(data);
$("#user_id").multiselect({
includeSelectAllOption: true,
selectAllJustVisible: false,
enableFiltering: true,
enableCaseInsensitiveFiltering: true,
buttonWidth: '407px'
});
});
});
});
});
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Departamento <span class="required"> * </span></label>
<div class="col-md-9 col-sm-9 col-xs-12">
<select id="project_id" name="project_id" class="form-control">
<option value="">-- Seleccionar Departamento --</option>
<?php while ($row = mysqli_fetch_assoc($result)) { ?>
<option value="<?php echo $row['id'] ?>">
<?php echo $row['name'] ?>
</option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Asignado a <span class="required"> * </span></label>
<div class="col-md-9 col-sm-9 col-xs-12">
<select id="user_id" name="user_id[]" multiple class="form-control">
</select>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>