always trigger “change”-event for <select>, even if the select2-option clicked is already selected

让人想犯罪 __ 提交于 2019-11-28 09:24:31

问题


I have a native <select>-element based on which I'm initializing a select2-dropdown-menu. I bound a change-event via select2 which is called whenever the select2/select-option is changed. However, I need to fire the event even if the currently selected option is selected again.

function onSelectChange(){
    alert('changed');
};

$("#select").select2().bind('change', onSelectChange);

I prepared a http://jsfiddle.net/rb6pH/1/ - if "Alaska" is currently selected and then selected again via the select2-dropdown, onSelectChange() should fire again, triggering the alert.

I have a hard time expressing myself, please ask if something isn't clear enough.


回答1:


A similar question was asked here. I have made a fiddle for your question based on the code in the link. http://jsfiddle.net/rb6pH/55/

function onSelectChange(){
alert('changed');
};

var select2 = $("#select").select2().data('select2');

select2.onSelect = (function(fn) {
return function(data, options) {
    var target;

    if (options != null) {
        target = $(options.target);
    }

    if (target) {
        onSelectChange();
        return fn.apply(this, arguments);
    }
}
})(select2.onSelect);



回答2:


Motivated by the absolutely valid remarks by @FritsvanCampen, I sat my ass down and figured out a way myself: What I really needed was a way to access the currently selected val, even if it hadn't changed. By using select2's undocumented close-event instead of change, I can access just that like so:

function onSelectChange(event){
    alert( $("#select").select2("val") );
};

$("#select").select2().bind('close', onSelectChange);

Find an updated jsFiddle at http://jsfiddle.net/rb6pH/42/




回答3:


I think you might refer to this link, i stuck once in same situation,

Change event is not fired when the selection is changed using Select2's val() method.

The event object contains the following custom properties:

val

the current selection (taking into account the result of the change) - id or array of ids.

added

the added element, if any - the full element object, not just the id

removed

the removed element, if any - the full element object, not just the id

For more information you might refer to this.

Thanks!




回答4:


I read the question and it's so close to my problem so I decided not to post another question. What I want to do is after select2 closed, a simple text input become focused but it doesn't work. please check

function onSelectChange(){
    alert('closed');
    $('#hey').focus();
};

$("#select").select2().bind('close', onSelectChange);
$('#hey').on('focusin)', function(){alert('in');}

http://jsfiddle.net/sobhanattar/x49F2/5/

I tried to make sure that the input become focused and it showed that the input become focused but I don't know why it doesn't show the curser




回答5:


 var prev_val = $('#select2').val();
 $('#select2').blur( function() {
    if( $(this).val() == prev_val )
       // not changed
    else {
       // changed
       prev_val = $(this).val();
    }
 });


来源:https://stackoverflow.com/questions/16486281/always-trigger-change-event-for-select-even-if-the-select2-option-clicked-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!