I am initializing select2(multiple) with remote data (json) and when the form is submitted select2 submits the indexes of the selected text(s), can it return selected text(in comma separated like indexes) instead of indexes?

 <input type="hidden" id="IntendedCourses" name="IntendedCourses"/>

 $.getJSON("GetCourses.aspx", function (course) {               
                $("#IntendedCourses").select2({
                    multiple: true,
                    data: course
                });    
            });

json format :

[{"id":"0","text":"Accounting"},{"id":"1","text":"Accounting & Finance"},{"id":"2","text":"Aeronautical"},{"id":"3","text":"Aerospace Engineering"}]
有帮助吗?

解决方案

I believe what you need is:

$("#IntendedCourses").change(function() {
    var selection = (JSON.Stringify($("#IntendedCourses").select2('data')));
});

This should give you the JSON for what is inside the selection upon change.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top