我正在使用select v4.0.3。如何获取select元素的替换/上一个值?我已经附加了一个'更改'监听器,但我似乎无法找到以前的值。
答案 0 :(得分:6)
可以使用select2:selecting
事件
请在此处查看代码: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());
});
答案 1 :(得分:3)
您可以在选择活动时轻松完成
$(this).on("select2-selecting", function (e) {
var curentValue = e.val;
var previousValue = $(this).val();
});
答案 2 :(得分:0)
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
});