jquery – 选择后触发一个动作select2

前端之家收集整理的这篇文章主要介绍了jquery – 选择后触发一个动作select2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
im使用select2 libary为我的搜索http://ivaynberg.github.io/select2/
有没有办法在选择搜索结果后触发一个动作?例如打开弹出窗口或简单的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,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
});

解决方法

请参阅 documentationfurther details section on events中的事件部分。

根据版本,下面的代码片段应该给你所需的事件,或者只是用“更改”替换“select2-selection”。@H_403_10@

版本4.0@H_403_10@

事件现在采用格式:select2:选择(而不是select2选择)@H_403_10@

感谢snakey的通知,这已更改为4.0@H_403_10@

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

版本4.0之前@H_403_10@

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

为了澄清,select2选择的文档:@H_403_10@

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()@H_403_10@

而变化有:@H_403_10@

change
Fired when selection is changed.@H_403_10@

因此,更改可能更适合您的需要,这取决于您是要选择完成,然后执行您的事件,或潜在地阻止更改。@H_403_10@

猜你在找的jQuery相关文章