jQuery .chosen.change() - 如何确定它是选择还是取消选择操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11678045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.chosen.change() - how to figure out whether it was a select or a deselect action
提问by oneiros
How can we figure out whether a change event triggered on a component was a result of a select or a deselect? How can we grab the value of the selected value only or the deselected value only?
我们如何确定在组件上触发的更改事件是选择还是取消选择的结果?我们如何仅获取选定值的值或仅获取取消选择的值?
$("#airports-select-2").chosen().change(function(event) {
console.log('select2 change', event, $(event.target).val());
});
This code gives me all the values that have been selected so far on a multiselect component? How can this be changed to only give me value of the newly selected or deselected component?
此代码为我提供了迄今为止在多选组件上选择的所有值?如何将其更改为仅提供新选择或取消选择的组件的值?
采纳答案by VulgarBinary
On every event fire you can trigger:
在每次触发事件时,您都可以触发:
var target = $(event.target),
priorDataSet = target.data("chosen-values"),
currentDataSet = target.val();
//Diff and compare the delta here.
target.data("chosen-values", currentDataSet);
Prior to the update of the data for the DOM element you can diff to determine what the delta is between the value sets. If it's in the old and not in the new it's removed. If it's in the new and not in the old then it's been added.
在更新 DOM 元素的数据之前,您可以通过差异来确定值集之间的增量。如果它是旧的而不是新的,它将被删除。如果它是新的而不是旧的,那么它已被添加。
If you need an example of how to determine the delta between two arrays let me know and I'll whip up an example for that as well.
如果您需要一个关于如何确定两个数组之间的增量的示例,请告诉我,我也会为此提供一个示例。
回答by Evgeny
From Chosen documentation:
从选择的文档:
Chosen triggers the standard DOM event whenever a selection is made (it also sends a selectedor deselectedparameter that tells you which option was changed).
每当进行选择时,Chosen 都会触发标准 DOM 事件(它还发送一个selected或deselected参数,告诉您哪个选项已更改)。
$('select').on('change', function(event, params) {
// can now use params.selected and params.deselected
});
回答by Tanmay D
The chosen documentation describes the Triggered Events
所选文档描述了触发事件
To catch the event on "search-choice-close", you can try the following:
要在“搜索选择关闭”上捕获事件,您可以尝试以下操作:
$('#selectID').on('change', function(change, deselected) { //selected OR deselected
//do something
console.log('Value Deselected');
});