简体   繁体   中英

How could I solve this onclick-onblur problem?

I have a form. In it I have a textarea which expands when onclick. But there is a little problem: When the user tries to submit the form (click the submit button), the textarea jumps back to its normal size. This is because it uses the onblur function. I want to eliminate this awkwardness, because the user has to click the submit button twice to submit the form.

What should I do to make it work with one click?

You can set a short timeout in the onblur handler that shrinks the text area:

document.getElementById("textareaID").onblur = function () {
    var target = this;
    setTimeout( function () {
        //Code to shrink the textarea, use "target" instead of "this" for scoping reasons.
    }, 250);

}
textArea.addEventListener('blur', function(e) {
    e.preventDefault();
    return false;
}, true);

This will add a listener to the blur event which will prevent anything else from firing, including whatever it is that's causing the textarea to re-resize. It'd be easier for you to not add the blur hook in the first place, rather than catching it this way, though.

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