在更改之前获取select(下拉列表)的值 [英] Getting value of select (dropdown) before change

查看:90
本文介绍了在更改之前获取select(下拉列表)的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现的是每当< select> 下拉列表更改时,我想要更改前的下拉列表的值。我正在使用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 (ie when i changed it to stack) i want it's previous value ie 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.

推荐答案

焦点事件与更改事件以实现您的目标:

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

这篇关于在更改之前获取select(下拉列表)的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆