문제

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