简体   繁体   中英

JS prevent firing focusout event if input didn't changed

I have the following input:

<input name="video" value="" type="text">

And attached js event:

input.focusout(function(){
        loadThumbnail();
    });

The problem is it triggers always when focus leaves field. Actually it's goods behavior, but didn't fit my needs, because if user didn't changed the field the event will be triggered and the request will be made on server.

I've tried to replace it with change event, but it doesn't triggers when user clean's field.

So what I need is event that will be triggered after user finished editing the field and detect cases when user cleans field or moves focus to/from it, but don't change anything.

Would you suggest a solution?

Try something like this to save the old value and compare it with new value:

var oldVal = ''
input.focusout(function () {
    var newVal = input.val();
    if (oldVal != newVal) {
        oldVal = newVal;
        loadThumbnail();
    }
});

Try the below part. You will have to tweak this. I just wrote a raw code for U.

var temp='';
input.focusin(function(){
     temp = input.val();
    });


input.focusout(function(){
        if temp != input.val() then
            loadThumbnail();
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM