Javascript 跟踪多选选项的上一个值
我有这三个选择,我想保存所有以前选择的选项,使用每一个,我已经为其中一个做了,但是有没有办法不为每个选项使用相同的代码?例如,使用每个 $function{ var-previous; $'dropdown1'.FocusFunction{ previous=$this.val; }; $'dropdown1'.ChangeFunction{ alertprevious值=+previous+,当前值=+$this.val; previous=$this.val; }; }; 10 20 30 40 50 60 70 80 90 尝试使用数组Javascript 跟踪多选选项的上一个值,javascript,jquery,Javascript,Jquery,我有这三个选择,我想保存所有以前选择的选项,使用每一个,我已经为其中一个做了,但是有没有办法不为每个选项使用相同的代码?例如,使用每个 $function{ var-previous; $'dropdown1'.FocusFunction{ previous=$this.val; }; $'dropdown1'.ChangeFunction{ alertprevious值=+previous+,当前值=+$this.val; previous=$this.val; }; }; 10 20 30
$(function() {
var previous = [];
$('#dropdown1').focus(function(e) {
var data = $(this).val();
previous.push(data);
});
});
现在所有的变化都将保留在您以前的
更新:如果要检索最后一个索引
$('#dropdown1').change(function(e) {
var last_index_value = previous[previous.lenght];
alert("previous value = " + last_index_value + ", current value = " + $(this).val());
previous.push($(this).val());
}) 使用数组为每次选择保留以前的值。和修改更改和焦点以针对所有选择元素,并从数组中引用/修改其各自的先前值:
var previous=[];
var selectelements = $('select');
$('select').focus(function(e){
previous[$('select').index(this)] = $(this).val();
});
$('select').change(function(e){
alert("previous value = " + previous[$('select').index(this)] + ", current value = " + $(this).val());
previous[$('select').index(this)] = $(this).val();
});
使用类选择器而不是ID
$function{
var-previous;
$'.ddl'.FocusFunction{
previous=$this.val;
};
$'.ddl'.ChangeFunction{
alertprevious值=+previous+,当前值=+$this.val;
previous=$this.val;
};
};
10
20
30
40
50
60
70
80
90
我认为这不是一个好主意,因为这样很难找回它们。太好了!我认为这一个更适合你me@reshad:这不会为每个选择引用正确的上一个值。@milindiantwar,但它似乎工作正常。但是你能解释一下你为什么这么说吗?我想看看你的观点和观点learn@reshad:每次聚焦时,它都会覆盖上一个值。是否确实不希望它们保持在阵列中各自的位置?如果不是,那么这就是你的解决方案