Trigger an action after selection select2

前端 未结 6 1075
盖世英雄少女心
盖世英雄少女心 2020-12-07 16:30

I am using select2 library for my search.
is there any way to trigger an action after selecting a search result? e.g. open a popup, or a simple js alert.



        
相关标签:
6条回答
  • 2020-12-07 16:48

    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
    });
    
    0 讨论(0)
  • 2020-12-07 16:57

    For above v4

    $('#yourselect').on("select2:select", function(e) { 
         // after selection of select2 
    });
    
    0 讨论(0)
  • 2020-12-07 16:59

    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 +

    Events are now in format: select2:selecting (instead of select2-selecting)

    Thanks to snakey for the notification that this has changed as of 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
    });
    

    Just to clarify, the documentation for select2-selecting reads:

    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.

    0 讨论(0)
  • 2020-12-07 17:02
    //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);
    });
    
    0 讨论(0)
  • 2020-12-07 17:03

    It works for me:

    $('#yourselect').on("change", function(e) { 
       // what you would like to happen
    });
    
    0 讨论(0)
  • 2020-12-07 17:07

    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

    0 讨论(0)
提交回复
热议问题