Resetting Jquery Select2 Multiple dropdownlist causes a blank item to be selected?

一笑奈何 提交于 2019-12-14 03:17:36

问题


I am using Multiple Select2 plugin in an user registraion page.On clicking the submit button,after the details are saved i am resetting the dropdownlist.The problem is that when i select another item from the dropdownlist a blank item is automatically selected in the dropdownlist

Html

  @Html.DropDownListFor(m => m.MultipleCourseId
                        , Model.MultipleCourseList
                        , ""
                       , new { @class = "form-control select2", @id = "ddlCourseMultiple",@multiple="multiple" })

Calling reset on Jquery Button click

 $(document).on("click", "#btnCancel", clearAll);
//clear all controls
var clearAll = function () {
    $(".form-control").val('');
    //resetting dpdwn for clearing the selected option        
    $("#ddlCourseMultiple").select2("val", "");
    return false;
};

回答1:


You could try this :

$("#ddlCourseMultiple").val(null).trigger("change");


来源:https://stackoverflow.com/questions/33119938/resetting-jquery-select2-multiple-dropdownlist-causes-a-blank-item-to-be-selecte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!