JQuery - Duplicate Field Input Text In Real Time

前端 未结 5 2065
盖世英雄少女心
盖世英雄少女心 2020-12-23 23:39

I\'m trying to figure out how to copy a users text input in one form field to another. Specifically, when someone fills in their email address in the contact form, it will b

5条回答
  •  醉梦人生
    2020-12-24 00:04

    You said you want it in real time. I assume that means while the user is typing, the value should be replicated for each keystroke.

    If that's right, try this:

    var mail = document.getElementById("mail");
    
    $("#email").keyup(function() {
        mail.value = this.value;
    });
    

    Or if you want more jQuery:

    var $mail = $("#mail");
    
    $("#email").keyup(function() {
        $mail.val( this.value );
    });
    

    This will update for each keyup event.

    I'd probably add a redundant blur event in case there's an autocomplete in the field.

    $("#email").blur(function() {
        $mail.val( this.value );
    });
    

提交回复
热议问题