Home >Web Front-end >JS Tutorial >How to Get the Value of a Dropdown Before It Changes in jQuery?
Obtaining Dropdown Value Before Change
In jQuery, the change event is often used to capture the value of a
One solution involves combining the focus and change events. When the dropdown receives focus, the current value can be stored in a variable. When the change event occurs, this stored value can be used to determine the value before the change.
Here's how you can implement this solution:
(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; }); })();
In this script:
This approach allows you to capture the value of the dropdown before it changes. Keep in mind that this technique should be applied to all
A live example of this solution can be found at the following URL: http://jsfiddle.net/x5PKf/766.
The above is the detailed content of How to Get the Value of a Dropdown Before It Changes in jQuery?. For more information, please follow other related articles on the PHP Chinese website!