Question

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"}]
Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top