简体   繁体   English

如何使用jquery查找多选标签的最新选定项?

[英]how to find the latest selected item of a multiple select tag using jquery?

I have a <select multiple="multiple" id="multi"> in my website, and I have a script in jquery that has this function: 我的网站上有一个<select multiple="multiple" id="multi"> ,我在jquery中有一个具有此功能的脚本:

$("#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? 我想访问的最新选定的项目select在这个函数(最后一个由用户选择的),我该怎么办呢?

You can use :selected selector and :last (or alternatively .last() ) this way: 您可以使用:selected selector和:last (或者.last() )这样:

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

JSFiddle 的jsfiddle

To get the last selected by the user, you can do this 要获取用户最后选择的内容,您可以执行此操作

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

    map = comp;

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

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

FIDDLE 小提琴

I think you are searching for this 我想你正在寻找这个

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

DEMO 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 : FIDDLE:

Less coding with Optimize solution : Why not use the is function of jquery: 使用Optimize解决方案减少编码:为什么不使用jquery的is函数:

$(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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery返回选择标记中所选项目的文本以及其他选择标记中所选项目的文本 - jquery returns the text of selected item in select tag plus text of selected item in other select tag 如何从“选择”标签中获取所选项目? - How to get the selected item from a 'select' tag? 捕获具有相同ID的多个选择标记中的选择标记的所选项目 - Capturing the selected item of select tag in multiple select tags with same id 如何使用JQuery / JSon从选择选项中获取选择的项目 - How to get selected item from select option using JQuery/JSon 嵌套在DataList标记中的jQuery Select-无法获取所选项目 - jQuery Select nested in DataList tag - can't get the selected item 如何使用 JavaScript\\JQuery 使用选择标记的选定选项的值设置隐藏输入标记的值? - How can I set the value of an hidden input tag with the value of the selected option of a select tag using JavaScript\JQuery? 如何使用jQuery在多个选择中取消选择所选选项 - How to deselect the selected option in multiple select using jQuery 避免使用jquery多次选择相同选项,但第一个选择标记除外 - Avoid same option selected multiple times using jquery except the first select tag 如何获得在多个选择框中选择的最新选项 - How to get the latest option selected in a multiple select box 如何使用 select<a>标签使用 jQuery</a> - How to select <a> tag using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM