简体   繁体   中英

How to get last selected item from chosen multiselect plugin

As the question says, I would like to know how to get the last selected item value from chosen multiselect plugin.

So far I have this:

$(document).ready(function() {
    $('#id').chosen().on('change', function(evt, param) {
            var id1 = this.value;
            var id2 = $("#id").chosen().val();
            var id3 = $('#id', this).filter(':selected:last').val();
            var id4 = $(evt.target).text();

            alert('IDs:' +id1+'-'+id2+'-'+id3+'-'+id4);
    });
});

Here is the output:

2 - undefined - undefined - [all the values]

The first answer is correct (I chose the second value).

But if I later choose the 3rd value or one after the one I chose before, it keeps showing me the first value I chose. However, if I choose a value situated before the first one it works perfectly and I don't know why.

Any help would be great.

EDIT:

This should work:

$('option').click(function(){
    var option = $(this);
    var str = "";
    $("#id option:selected" ).each(function() {
        if (option.val()==$(this).val()){
            alert (option.val());
        }
    });
});

See this jsfiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM