Get last selected value of multiple select element

前端 未结 4 1033
渐次进展
渐次进展 2021-01-05 01:38

I have a number of select boxes that have been set to accept multiple values. Is there anyway that I can use jQuery to get the last selected value? I\'ve tried using the f

4条回答
  •  盖世英雄少女心
    2021-01-05 02:04

    Here is the code to get the latest selected option from multiple select list using Jquery :

    var selected;
    $(document).ready(function() {
    $('#SELECT_ID').on('click', function() {
    var latestSelected;
    if (selected) {
        var currentOptionsValue = $(this).val();
        latestSelected = currentOptionsValue.filter(function(element) {
        return selected.indexOf(element) < 0;
      });
    }
    selected = $(this).val();
    console.log( "latest selected option : " + latestSelected);
    });
    });
    

提交回复
热议问题