在我的代码中,我使用多个select2选择一个值。但是,问题在于您无法在选择中更改顺序。有人可以帮我吗?
<select class="form-control col-10" id="questions" name="questions[]" multiple="multiple">
@foreach($tasks as $t)
@if(!$t->trashed() || in_array($t->id, $lesson->tasksId()))
<option value="{{$t->id}}" @if(in_array($t->id, $lesson->tasksId())) selected @endif>{{$t->vocabulary->native_text}}</option>
@endif
@endforeach
</select>
<script type="text/javascript">
$(document).ready(function(){
$('#questions').select2();
$('#lessonLevelEdit').on('change', loadOtherTasks);
});
function loadOtherTasks() {
const newVal = $('#lessonLevelEdit').val();
$.get(`/api/tasks?level=${newVal}`, function(data) {
// Reformat data to select2 structure
const dataReformat = data.map(item => {
return {
'id': item.id,
'text': item.vocabulary_nt
}
});
// Remove old options (previous level)
$('#questions option').remove();
$('#questions').select2({
data:dataReformat
});
});
}
</script>