简体   繁体   English

获取触发多重选择onchange事件的选项的值和状态(选定与否)

[英]get the value and status (selected or not) of the option that triggers a multiple select onchange event

I want to get only the value added/removed from the selection instead of an array with all the selected values. 我想只从选择中添加/删除值而不是包含所有选定值的数组。

How can I monitor each individual option instead of the whole selector to see which one has changed? 如何监控每个选项而不是整个选择器以查看哪个选项已更改?

I know I can compare the previous and current state of the whole selector, but that means saving the previous state, and since it is not just one selector I want to keep track of but an undetermined number of them (one per record on a list of records retrieved from the database), I would very much rather use a simpler way to get the last selected/unselected value if there is one. 我知道我可以比较整个选择器的先前和当前状态,但这意味着保存以前的状态,因为它不仅仅是一个我想跟踪的选择器,而是一个未确定的数量(列表中每个记录一个)从数据库中检索到的记录),我宁愿使用更简单的方法来获取最后一个选中/未选中的值(如果有的话)。

Another alternative I just thought of: Instead of keeping track of every selector state, using the onfocus event to save the state of the selector about to be modified (but still, I would prefer a solution where I do not have to compare states, if there is one). 我想到的另一种选择:不使用onfocus事件来保存选择器的状态,而是跟踪每个选择器状态,而不是保留选择器的状态(但仍然,我更喜欢一种解决方案,我不需要比较状态,如果有一个)。

Current solution: use bootstrap multiselect jquery plugin's onchange option (bootstrap multiselect turns the selector into a list so each option can be monitored individually using its own onchange event). 当前解决方案:使用bootstrap multiselect jquery插件的onchange选项 (bootstrap multiselect将选择器转换为列表,以便可以使用自己的onchange事件单独监视每个选项)。 Any "pluginless" ideas? 任何“无插件”的想法?

I don't think there exists some decent solution. 我认为不存在一些体面的解决方案。 Roll your own jQuery plugin to abstract state comparison. 滚动您自己的jQuery插件以抽象状态比较。

Maybe this little plugin which triggers custom optionChange event on list, providing two additional parameters (value, state) can help you: 也许这个小插件触发列表上的自定义optionChange事件,提供两个额外的参数(值,状态)可以帮助您:

$.fn.trackOptions = function(){
    return this.each(function(){
        var $select = $(this),
            oldItems = $select.val() || [];
        $select.data("oldItems", oldItems)
        .on("change", function(e){
            var oldItems = $select.data("oldItems"),
                newItems = $select.val() || [];
            for(var i=0; i<oldItems.length; ++i) {
                if(newItems.indexOf(oldItems[i]) == -1){
                    $select.trigger("optionChange", [oldItems[i], false]);
                }
            }
            for(i=0; i<newItems.length; ++i){
                if(oldItems.indexOf(newItems[i]) == -1){
                    $select.trigger("optionChange", [newItems[i], true]);
                }
            }
            $select.data("oldItems", newItems);
        });
    });
}

jsFiddle 的jsfiddle

If needed, you can modify it to trigger single event with list of removed options. 如果需要,您可以修改它以触发具有已删除选项列表的单个事件。

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

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