Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何按单击顺序获取多选列表框的值?_Javascript_Jquery - Fatal编程技术网

Javascript 如何按单击顺序获取多选列表框的值?

Javascript 如何按单击顺序获取多选列表框的值?,javascript,jquery,Javascript,Jquery,我有一个multiselect列表框,并使用 $('[id*=ListBox]').click(function(){ var selectedItems = $(this).val(); var lastItem = $("[id*=ListBox] option:selected").last().val(); }); 这将返回选定值的逗号分隔数组。但问题是数组总是按值排序,而不是按单击选定值的顺序生成 3000300530093011 但如果我首先单击值为3011、3

我有一个multiselect列表框,并使用

$('[id*=ListBox]').click(function(){

    var selectedItems = $(this).val();
    var lastItem = $("[id*=ListBox] option:selected").last().val();

});
这将返回选定值的逗号分隔数组。但问题是数组总是按值排序,而不是按单击选定值的顺序生成

3000300530093011

但如果我首先单击值为3011、3005、3000和最后3009的项,我希望按该顺序单击值

3011300530003009

如何按单击顺序获取选定值

编辑


选择最近的值也解决了我的问题


如何获取最近选定的项目?

首先,设置一个事件,每当用户单击时向每个列表框项目添加一个整数。将整数存储在页面上某个隐藏元素中,或者通过在元素上设置如下数据属性来执行一些巧妙的操作:

$(function() {
    $("#ListBox").data("selected-index", 0);
});

$("#ListBox option").on("click", function() {
    var currentSelectedIndex = $("#ListBox").data("selected-index");
    $(this).data("counter", currentSelectedIndex++);
});
然后,为了按单击顺序获取所有这些内容:

function getOrderOfItems() {
    var arrayOfSelected = new Array(),

    // Use Math and .map() to get the highest data-counter:

    counters = $("#ListBox option[data-counter]").map(function() {
        return parseInt($(this).data("counter"), 10);
    }).get();

    var highestCounter = Math.max.apply(Math, counters);

    // We have the highest counter, so use a for loop to select the selected options.
    for (var i = 0; i < highestCounter; i++) {
        arrayOfSelected.push($("#ListBox option[data-counter=" + i + "]"));
    }

    console.log(arrayOfSelected);
    return arrayOfSelected;
}
其中arrayOfSelected按单击顺序包含列表框项目。

注意,使用了@PaulRoub答案中的html,因为在OP中没有显示任何html

如果正确解释问题,尝试用更改事件替换单击事件;创建选定值的数组,利用.slice-2[0]查看以前选定的项

$(document).ready(function () {
            var arr = [];
            $("[id*=listBox]").click(function () {
                var lastItem = '';
                var selArr = [];
                var flag = 0;

                selArr = $(this).val();
               // alert(selArr);
                if ( $(this).val()!=null) {
                    if ($(this).val().length > 2) {

                        lastItem = $(selArr).not(arr).get();

                        selArr = $.grep(selArr, function (value) {
                            return value != lastItem;
                        });
                        flag = 1;
                        //arr = [];
                    }
                }
                arr = selArr;
                $(this).val(selArr);

                if (flag == 1)
                    alert("Cannot select more than 2 items");

            });
        });
$ListBox.dataprev,[].changefunctione{ $this.data.prev.pushthis.value console.log$this.data.prev.slice-2[0],//最后选择的项 $this.data.prev//按选定顺序排列的选定项目数组 } 3000 3001 3002 3003 3004
这就是我的工作。我必须获取一个全局数组,每次单击后将listbox的选定项与数组中的元素进行比较。他们之间的差异给了我最新的选择项目

$(document).ready(function () {
            var arr = [];
            $("[id*=listBox]").click(function () {
                var lastItem = '';
                var selArr = [];
                var flag = 0;

                selArr = $(this).val();
               // alert(selArr);
                if ( $(this).val()!=null) {
                    if ($(this).val().length > 2) {

                        lastItem = $(selArr).not(arr).get();

                        selArr = $.grep(selArr, function (value) {
                            return value != lastItem;
                        });
                        flag = 1;
                        //arr = [];
                    }
                }
                arr = selArr;
                $(this).val(selArr);

                if (flag == 1)
                    alert("Cannot select more than 2 items");

            });
        });
这个问题让我发现了两件奇怪的事情

$Listbox option.click不会在Internet explorer上启动我使用的是版本9,但是 对其他人很好。我不知道为什么选项元素中没有 我收到了一封信。 $Listbox.val以逗号分隔的形式列出了 排序顺序,而不是选择项目的顺序。这 事实证明,这是一个重大的惊喜和头痛。
你能添加一个演示吗?选择最新的值也解决了我的问题。如何获取最新的选定项?你可以保留一个隐藏的输入,并在单击事件时将隐藏字段中的值设置为最新值。因此,在上面的示例中,我正在单击值为3009的项。如何获得3009值可以包含html的问题?