我正在使用select2 library进行搜索.
有没有办法在 Select 搜索结果后触发操作?例如打开弹出窗口或简单的JSalert .

$("#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
});

推荐答案

See the documentation events section

Depending on the version, one of the snippets below should give you the event you want, alternatively just replace "select2-selecting" with "change".

Version 4.0 +

事件现在的格式为:select2:selecting(而不是select2-selecting)

感谢snakey的通知,从4.0开始,这一点已经发生了变化

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

Version Before 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可能更适合您的需要,这取决于您是希望完成 Select ,然后进行活动,还是可能阻止更改.

Jquery相关问答推荐

ASP.NET Core 6 jQuery 验证不起作用

如果选中了三个sibling ,则选中一个复选框

在 CSS3 或 jQuery 中将元素的宽度从 0% 设置为 100% 动画,它和它的包装器只需要它们需要的宽度,没有预先设置的宽度

jQuery javascript 正则表达式 将
替换为 \n

使用 jQuery 的并行异步 Ajax 请求

jQuery 的 ajax 默认超时值是多少?

如何在 Slick 轮播项目之间添加空格

如何在单击时 Select 多选 Select 框的所有选项?

找到下一个匹配的sibling 姐妹的有效,简洁的方法?

如何正确卸载/销毁 VIDEO 元素

按百分比zoom div的内容?

你如何获得文本区域中的光标位置?

为什么 Chrome 会忽略本地 jQuery cookie?

如何阻止 Chrome 进入调试模式?

如何使用jquery获取点击链接的href?

将片段添加到 URL 而不导致重定向?

判断文本框是否有空值

jQuery确定匹配的类是否具有给定的ID

jQuery attr 与props ?

bootstrap 日期和时间 Select 器