最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to find the latest selected item of a multiple select tag using jquery? - Stack Overflow

programmeradmin3浏览0评论

I have a <select multiple="multiple" id="multi"> in my website, and I have a script in jquery that has this function:

$("#multi").on("change",function(){}) 

I want to access the latest selected item of the select in this function(the last one selected by user), how can I do that?

I have a <select multiple="multiple" id="multi"> in my website, and I have a script in jquery that has this function:

$("#multi").on("change",function(){}) 

I want to access the latest selected item of the select in this function(the last one selected by user), how can I do that?

Share Improve this question edited Sep 6, 2014 at 18:29 animuson 54.8k28 gold badges142 silver badges150 bronze badges asked Aug 20, 2014 at 6:04 Navid777Navid777 3,6818 gold badges43 silver badges72 bronze badges 3
  • The last, as in the last one selected by the user, or the last selected option in the HTML etc. – adeneo Commented Aug 20, 2014 at 6:12
  • I mean the last one selected by user – Navid777 Commented Aug 20, 2014 at 6:23
  • Then you should probably answer the ments when they are posted, and test the answers before you accept them – adeneo Commented Aug 20, 2014 at 6:25
Add a ment  | 

6 Answers 6

Reset to default 6

To get the last selected by the user, you can do this

var map = $("#multi").on("change",function(){
    var p = $("#multi option:selected").map(function() {
            return this.value;
        }).get(),
        set1 = map.filter(function(i) {
            return p.indexOf(i) < 0;
        }),
        set2 = p.filter(function(i) {
            return map.indexOf(i) < 0;
        }),
        last = (set1.length ? set1 : set2)[0];

    map = p;

    // "last" contains the last one selected /unselected

}).find('option:selected').map(function() {return this.value}).get();

FIDDLE

You can use :selected selector and :last (or alternatively .last()) this way:

$('#multi').change(function() {
  console.log( $(this).find('option:selected:last').text() );
});

JSFiddle

I think you are searching for this

$("#multi").on("change",function(){
    alert($("option:selected:last",this).val());    
}) 

DEMO

var last_value;   

 $('#multi').change(function() {
     last_value = this.val();
alert(last_value);
    });

define global variable, and every time ur dropdown change the seletion, u can replace the last selected value with the new one.

FIDDLE :

Less coding with Optimize solution : Why not use the is function of jquery:

$(document).ready(function(){
    $('#multi option').click(function(){        
        if($(this).is(':selected'))
            console.log('you have select' + $(this).val());
        else
            console.log('you have Unselect' + $(this).val());
    });
});

How about this

var = last;
$('#multi').find('option').each(function() {
    $(this).click(function() {
        if($(this).hasAttr('selected')) {
            last = $(this);
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论