【问题标题】:Trigger an action after selection select2选择select2后触发动作
【发布时间】:2013-09-08 00:50:59
【问题描述】:

我正在使用select2 library 进行搜索。
选择搜索结果后有什么方法可以触发动作吗?例如打开一个弹出窗口,或者一个简单的 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
});

【问题讨论】:

  • 您可以绑定到“更改”事件,在您提供的链接上有一个名为“事件”的部分,其中包含用于所有不同事件绑定的非常长的代码 sn-p。

标签: jquery search jquery-select2


【解决方案1】:

documentation events section

根据版本,下面的 sn-ps 之一应该会为您提供所需的事件,或者只需将“select2-selecting”替换为“change”。

版本 4.0 +

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

感谢 snakey 通知,从 4.0 起这已更改

$('#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-选择 在选择一个选项时触发 下拉菜单,但在对选择进行任何修改之前。 此事件用于允许用户通过调用拒绝选择 event.preventDefault()

而变化有:

改变 更改选择时触发。

所以change 可能更适合您的需求,具体取决于您是希望完成选择然后执行您的活动,还是可能阻止更改。

【讨论】:

  • 事件在新版本中有其他名称(例如'select2:select'),请参阅https://select2.github.io/examples.html
  • 事件是文档中的here。 FAQ 风格并不明显。
  • “选择”调用在设置值之前完成。这将导致:“console.log($(this).val())”输出一个空白值
  • 当我在我的角度应用程序中尝试相同时,它给了我错误的值。假设 select2 选项中有 4 个值。每天 1 分钟、5 分钟、1 小时。现在使用 $('#yourselect').on("select2-selecting", function(e) {...} 它给了我以前选择的值。
  • 我尝试了 $('#yourselect').on("select2-select"),function(e){...} 而不是那对我有用
【解决方案2】:

对 select2 事件名称进行了一些更改(我认为是在 v. 4 及更高版本),因此 '-' 更改为此 ':'
请参阅下一个示例:

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

您可以在“select2”插件站点查看所有事件: select2 Events

【讨论】:

    【解决方案3】:

    它对我有用:

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

    【讨论】:

    • 我不明白为什么,但是当 Tarek 的回答不起作用时,这对我有用。我什至得到了他在一个单独项目中工作的答案,但由于某种原因,这是让我的项目启动并运行的唯一覆盖。
    • @tokyo0709 之所以有效,是因为.change 事件在一系列执行中比select2:select 事件晚。对我来说,我试图在 select2:select 上捕获选择的 css,但即使我在检查员上看到它,我也无法通过 select2:select 捕获它 - 当我更改为 .change 时它起作用了,因为 Select2 触发了 select2:select 事件 之前更新原始选择的css..在我看来有点疯狂。
    • @a20 有时一个简单的解决方法是通过 html css 设置 dom 元素 css,这样您就不必等待 javascript 引擎赶上。在某些情况下效果很好。
    【解决方案4】:

    根据我在 v.4 以上的用法,这会起作用

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

    不到 v.4 就可以了:

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

    【讨论】:

    • 这对我不起作用。我已将上面的代码更改为如下所示。 e.params.args.data
    • 这对我也不起作用,e.params.data 通过未找到未定义的数据字段。经过仔细检查,没有为 e 设置 params 字段。对于我的问题,我只需要读取值,所以 e.val 对我来说就足够了。
    【解决方案5】:

    这对我有用(Select2 4.0.4):

    $(document).on('change', 'select#your_id', function(e) {
        // your code
        console.log('this.value', this.value);
    });
    

    【讨论】:

      【解决方案6】:

      v4以上

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

      【讨论】:

        【解决方案7】:
        //when a Department selecting
        $('#department_id').on('select2-selecting', function (e) {
            console.log("Action Before Selected");
            var deptid=e.choice.id;
            var depttext=e.choice.text;
            console.log("Department ID "+deptid);
            console.log("Department Text "+depttext);
        });
        
        //when a Department removing
        $('#department_id').on('select2-removing', function (e) {
            console.log("Action Before Deleted");
            var deptid=e.choice.id;
            var depttext=e.choice.text;
            console.log("Department ID "+deptid);
            console.log("Department Text "+depttext);
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-10-20
          • 1970-01-01
          相关资源
          最近更新 更多