how to refresh Select2 dropdown menu after ajax loading different content?

后端 未结 7 703
夕颜
夕颜 2020-12-08 19:42

I\'m using Select2 in a combination of dropdown menus. I have one menu for \"Countries\" and one for \"States/Provinces\". Depending on the country that is chosen, the \"Sta

7条回答
  •  盖世英雄少女心
    2020-12-08 20:13

    Select 3.*

    Please see Update select2 data without rebuilding the control as this may be a duplicate. Another way is to destroy and then recreate the select2 element.

    $("#dropdown").select2("destroy");
    
    $("#dropdown").select2();
    

    If you are having problems with resetting the state/region on country change try clearing the current value with

    $("#dropdown").select2("val", "");
    

    You can view the documentation here http://ivaynberg.github.io/select2/ that outlines nearly/all features. Select2 supports events such as change that can be used to update the subsequent dropdowns.

    $("#dropdown").on("change", function(e) {});
    

    Select 4.* Update

    You can now update the data/list without rebuilding the control using:

    fooBarDropdown.select2({
        data: fromAccountData
    });
    

提交回复
热议问题