Getting value of select (dropdown) before change

后端 未结 18 2461
无人共我
无人共我 2020-11-22 08:56

The thing I want to achieve is whenever the

提交评论

  • 2020-11-22 09:10
     $("#dropdownId").on('focus', function () {
        var ddl = $(this);
        ddl.data('previous', ddl.val());
    }).on('change', function () {
        var ddl = $(this);
        var previous = ddl.data('previous');
        ddl.data('previous', ddl.val());
    });
    
    0 讨论(0)
  • 2020-11-22 09:10

    Use following code,I have tested it and its working

    var prev_val;
    $('.dropdown').focus(function() {
        prev_val = $(this).val();
    }).change(function(){
                $(this).unbind('focus');
                var conf = confirm('Are you sure want to change status ?');
    
                if(conf == true){
                    //your code
                }
                else{
                    $(this).val(prev_val);
                    $(this).bind('focus');
                    return false;
                }
    });
    
    0 讨论(0)
  • 2020-11-22 09:10

    var last_value;
    var current_value;
    $(document).on("click","select",function(){
        last_value = $(this).val();
    }).on("change","select",function(){
        current_value = $(this).val();
    
        console.log('last value - '+last_value);
        console.log('current value - '+current_value);
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <select name="test">
    <option value="stack">Stack</option>
    <option value="overflow">Overflow</option>
    <option value="my">My</option>
    <option value="question">Question</option>
    </select>

    0 讨论(0)
  • 2020-11-22 09:13

    How about using a custom jQuery event with an angular watch type interface;

    // adds a custom jQuery event which gives the previous and current values of an input on change
    (function ($) {
        // new event type tl_change
        jQuery.event.special.tl_change = {
            add: function (handleObj) {
                // use mousedown and touchstart so that if you stay focused on the
                // element and keep changing it, it continues to update the prev val
                $(this)
                    .on('mousedown.tl_change touchstart.tl_change', handleObj.selector, focusHandler)
                    .on('change.tl_change', handleObj.selector, function (e) {
                    // use an anonymous funciton here so we have access to the
                    // original handle object to call the handler with our args
                    var $el = $(this);
                    // call our handle function, passing in the event, the previous and current vals
                    // override the change event name to our name
                    e.type = "tl_change";
                    handleObj.handler.apply($el, [e, $el.data('tl-previous-val'), $el.val()]);
                });
            },
            remove: function (handleObj) {
                $(this)
                    .off('mousedown.tl_change touchstart.tl_change', handleObj.selector, focusHandler)
                    .off('change.tl_change', handleObj.selector)
                    .removeData('tl-previous-val');
            }
        };
    
        // on focus lets set the previous value of the element to a data attr
        function focusHandler(e) {
            var $el = $(this);
            $el.data('tl-previous-val', $el.val());
        }
    })(jQuery);
    
    // usage
    $('.some-element').on('tl_change', '.delegate-maybe', function (e, prev, current) {
        console.log(e);         // regular event object
        console.log(prev);      // previous value of input (before change)
        console.log(current);   // current value of input (after change)
        console.log(this);      // element
    });
    
    0 讨论(0)
  • 2020-11-22 09:16

    I go for Avi Pinto's solution which uses jquery.data()

    Using focus isn't a valid solution. It works at first time you change the options, but if you stay on that select element, and press key "up" or "down". It won't go through the focus event again.

    So the solution should be more looks like the following,

    //set the pre data, usually needed after you initialize the select element
    $('mySelect').data('pre', $(this).val());
    
    $('mySelect').change(function(e){
        var before_change = $(this).data('pre');//get the pre data
        //Do your work here
        $(this).data('pre', $(this).val());//update the pre data
    })
    
    0 讨论(0)
  • 提交回复
    热议问题