jQuery select2刷新数据 [英] jquery select2 refresh data

查看:227
本文介绍了jQuery select2刷新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用select2选择并且ajax加载数据,当初始选择后状态发生变化时,如何替换旧数据.

I using select2 to select and ajax to load data, how can I replace old data when the state is changed after initial selection.

我的加载代码:

$('.datetimepicker').on("changeDate", function() {  
        //refresh selectbox ??
        doctor_id = $(".select2-doctors").val();
        clinic_id = $(".select2-clinics").val();
        date = $('.datetimepicker').datepicker('getFormattedDate');
        $.ajax({
            url: '/admin/appointments/time_slots',
            type: 'GET',
            dataType: 'json',
            data: {doctor_id: doctor_id, clinic_id: clinic_id, date: date}
        })
        .done(function(data) {
            console.log("success");
            console.log(data);
            $('.select2-slot').select2({
                data: data.slots
            });
        })
        .always(function() {
            console.log("complete");
        });
    });

推荐答案

在更新数据之前,请尝试清空select2选项,这是因为默认情况下会将数据附加到现有选项中.

Try emptying the select2 options before to update data, this because by default it appends data to existing options.

$('.select2-slot').empty();
$('.select2-slot').select2({
    data: data.slots
});

这篇关于jQuery select2刷新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆