使用onchange事件在Jquery中存储数据

时间:2012-11-21 01:28:47

标签: jquery html

扩展我的上一个问题如何从我的下拉列表中存储最后选择的值,但每次用户更改其选择时我保持(先前选择的值)和(当前所选值)。我是否需要使用数组并使用push / pop或者还有另一种方法吗?

$(document).on('change', 'select[name=options]', function() {

var prev_selected =  $(this).val();
$(this).data('prev_selected',prev_selected);
alert(prev_selected );


//alert(current_selected );

});

HTML

<table id="Final">
<thead>
<th>Tie</th> <th>Pie</th><th>Lie</th>
</thead>

<tr>
<td><select class="Member" name="options">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">ball</option>
    <option value="4">4</option></td>
    <td>
    <select class="Member" name="options" price="200">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option></td>

    <td>
    <select class="Member" name="options" price="200">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">shoe</option>
    <option value="4">4</option></td>

    </tr>

    </table>

1 个答案:

答案 0 :(得分:0)

您基本上希望捕获焦点上的前一个值,该值在更改之前发生。

以下是回答您问题的类似帖子:Getting value of select (dropdown) before change

(function () {
    var previous;

    $("select").focus(function () {
        // Store the current value on focus, before it changes
        previous = this.value;
    }).change(function() {
        // Do something with the previous value after the change
        alert(previous);
    });
})();