Programing

선택 후 작업 트리거 select2

lottogame 2020. 11. 10. 07:42
반응형

선택 후 작업 트리거 select2


내가 사용하고 선택 2 라이브러리를 내 검색.
검색 결과를 선택한 후 작업을 트리거하는 방법이 있습니까? 예를 들어 팝업 또는 간단한 js 경고를 엽니 다.

$("#e6").select2({
    placeholder: "Enter an item id please",
    minimumInputLength: 1,
    ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
        url: "index.php?r=sia/searchresults",
        dataType: 'jsonp',
        quietMillis: 3000,
        data: function (term, page) {
        return {
            q: term, // search term
            page_limit: 10,
            id: 10
            };
        },
        results: function (data, page) { // parse the results into the format expected by Select2.
            // since we are using custom formatting functions we do not need to alter remote JSON data
            return {results: data};
        },
    },

    formatResult: movieFormatResult, // omitted for brevity, see the source of this page
    formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page
    dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
    escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
});

문서 이벤트 섹션을 참조하십시오.

버전에 따라 아래 스 니펫 중 하나가 원하는 이벤트를 제공해야합니다. 또는 "select2-selecting"을 "change"로 바꾸십시오.

버전 4.0 이상

이제 이벤트 형식 : select2:selecting(대신 select2-selecting)

이것이 4.0에서 변경되었다는 알림에 대해 snakey에게 감사드립니다.

$('#yourselect').on("select2:selecting", function(e) { 
   // what you would like to happen
});

4.0 이전 버전

$('#yourselect').on("select2-selecting", function(e) { 
   // what you would like to happen
});

명확히하기 위해 문서는 select2-selecting다음과 같습니다.

select2-selecting Fired when a choice is being selected in the dropdown, but before any modification has been made to the selection. This event is used to allow the user to reject selection by calling event.preventDefault()

whereas change has:

change Fired when selection is changed.

So change may be more appropriate for your needs, depending on whether you want the selection to complete and then do your event, or potentially block the change.


There was made some changes to the select2 events names (I think on v. 4 and later) so the '-' is changed into this ':'.
See the next examples:

$('#select').on("select2:select", function(e) { 
    //Do stuff
});

You can check all the events at the 'select2' plugin site: select2 Events


It works for me:

$('#yourselect').on("change", function(e) { 
   // what you would like to happen
});

As per my usage above v.4 this gonna work

$('#selectID').on("select2:select", function(e) { 
    //var value = e.params.data;  Using {id,text format}
});

And for less then v.4 this gonna work:

$('#selectID').on("change", function(e) { 
   //var value = e.params.data; Using {id,text} format
});

For above v4

$('#yourselect').on("select2:select", function(e) { 
     // after selection of select2 
});

참고URL : https://stackoverflow.com/questions/18615108/trigger-an-action-after-selection-select2

반응형