简体   繁体   English

Select2:如何获取以前选择的值

[英]Select2: How to get previous selected values

I am using select v4.0.3.我正在使用选择 v4.0.3。 How do I get the replaced/previous value of the select element?如何获取 select 元素的替换/先前值? I already attached a 'change' listener but I can't seem to find the previous value.我已经附加了一个“更改”侦听器,但似乎找不到以前的值。

Previously selected value can be acquired by using select2:selecting event可以使用select2:selecting事件获取之前选择的值

see the codes here: https://codepen.io/jacobgoh101/pen/ZpGvkx?editors=1111请参阅此处的代码: https : //codepen.io/jacobgoh101/pen/ZpGvkx?editors=1111

$('select').on('select2:selecting', function (evt) {
  console.log('previously selected ' + $('select').val());
});
$('select').on('select2:select', function (evt) {
  console.log('now selected ' + $('select').val());
});

You can do it easily in selecting event您可以轻松地选择事件

$(this).on("select2-selecting", function (e) {
    var curentValue = e.val;
    var previousValue = $(this).val();
});
var oldname;//global declaration
$('select').on('select2:selecting', function (evt) {
  oldName= $('select').val();
});
$(select).on("select2:select select2:unselecting", function (e) {
   //For selecting the new option
   var currentName= $(this).select2('data')[0].text;
   console.log(oldName);//you can get old name here also
});

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

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