How can I check if a value is changed on blur event?

前端 未结 10 1209
时光说笑
时光说笑 2021-02-07 06:29

Basically I need to check if the value is changed in a textbox on the \'blur\' event so that if the value is not changed, I want to cancel the blur event.

If it possible

相关标签:
10条回答
  • 2021-02-07 06:30

    Similar to @Kevin Nadsady's post, the following will work in native JS functions and JQuery listener events. Within the onblur event, you can compare the value against the defaultValue:

     $(".saveOnChange").on("blur", function () {
         if (this.value != this.defaultValue) {
             //Set the default value to the new value
             this.defaultValue = this.value;
             //todo: save changes
             alert("changed");
         }
     });
    
    0 讨论(0)
  • 2021-02-07 06:36

    You can use this code:

    var Old_Val;
    var Input_Field = $('#input');
    
    Input_Field.focus(function(){
         Old_Val = Input_Field.val();
    });
    
    Input_Field.blur(function(){
        var new_input_val = Input_Field.val();
        if (new_input_val != Old_Val){
              // execute you code here
        }
    });
    
    0 讨论(0)
  • 2021-02-07 06:46

    Using Jquery events we can do this logic

    Step1 : Declare a variable to compare the value

    var  lastVal ="";
    

    Step 2: On focus get the last value from form input

      $("#validation-form :input").focus(function () {
                     lastVal = $(this).val();
    
      });
    

    Step3:On blur compare it

    $("#validation-form :input").blur(function () {
                     if (lastVal != $(this).val())
                     alert("changed");
                 });
    
    0 讨论(0)
  • 2021-02-07 06:47

    You can't cancel the blur event, you need to refocus in a timer. You could either set up a variable onfocus or set a hasChanged variable on the change event. The blur event fires after the change event (unfortunately, for this situation) otherwise you could have just reset the timer in the onchange event.

    I'd take an approach similar to this:

    (function () {
        var hasChanged;
        var element = document.getElementById("myInputElement");
        element.onchange = function () { hasChanged = true; }
        element.onblur = function () {
            if (hasChanged) {
                alert("You need to change the value");
    
                // blur event can't actually be cancelled so refocus using a timer
                window.setTimeout(function () { element.focus(); }, 0);          
            }
            hasChanged = false;
        }
    })();
    
    0 讨论(0)
  • 2021-02-07 06:47

    Why not just maintaining a custom flag on the input element?

    input.addEventListener('change', () => input.hasChanged = true);
    input.addEventListener('blur', () => 
    {
        if (!input.hasChanged) { return; }
        input.hasChanged = false;
        // Do your stuff
    });
    

    https://jsfiddle.net/d7yx63aj

    0 讨论(0)
  • 2021-02-07 06:50

    I don't think there is a native way to do this. What I would do is, add a function to the focus event that saves the current value into a variable attached to the element (element.oldValue = element.value). You could check against that value onBLur.

    0 讨论(0)
提交回复
热议问题