質問

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