问题描述
我想要实现的事情是每当 下拉列表发生更改时,我希望在更改之前下拉列表的值.我正在使用 1.3.2 版本的 jQuery 并在更改事件上使用,但我在那里得到的值是在更改之后.
The thing I want to achieve is whenever the <select>
dropdown is changed I want the value of the dropdown before change. I am using 1.3.2 version of jQuery and using on change event but the value I am getting over there is after change.
<select name="test">
<option value="stack">Stack</option>
<option value="overflow">Overflow</option>
<option value="my">My</option>
<option value="question">Question</option>
</select>
假设当前选项 My 现在被选中,当我在 onchange 事件中将其更改为堆栈时(即当我将其更改为堆栈时)我想要它的先前值,即我在这种情况下的预期值.
Lets say currently option My is selected now when I change it to stack in the onchange event (i.e. when I changed it to stack) I want it's previous value i.e. my expected in this case.
如何实现?
就我而言,我在同一页面中有多个选择框,并且希望将相同的内容应用于所有选择框.此外,我的所有选择都是在通过 ajax 加载页面后插入的.
In my case I am having multiple select boxes in the same page and want same thing to be applied to all of them. Also all of my select are inserted after page load through ajax.
推荐答案
将 focus 事件与 change 事件结合起来以实现您想要的:
Combine the focus event with the change event to achieve what you want:
(function () {
var previous;
$("select").on('focus', function () {
// Store the current value on focus and on change
previous = this.value;
}).change(function() {
// Do something with the previous value after the change
alert(previous);
// Make sure the previous value is updated
previous = this.value;
});
})();
工作示例:http://jsfiddle.net/x5PKf/766
这篇关于在更改之前获取选择(下拉)的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!