JQuery - Duplicate Field Input Text In Real Time

前端 未结 5 2066
盖世英雄少女心
盖世英雄少女心 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 );
    });
    
    0 讨论(0)
  • 2020-12-24 00:06

    Since all your fields have unique ids, this is quite straight forward:

    $(function() {                                       // <== Doc Ready
        $("#email").change(function() {                  // When the email is changed
            $('#mail').val(this.value);                  // copy it over to the mail
        });
    });
    

    Try it out with this jsFiddle


    .change()
    .val()

    0 讨论(0)
  • 2020-12-24 00:13

    Is $("#mail") another input box ? It doesn't appear in your HTML (Edit: well it does now, but didn't at first :)

    $("#mail").val($("#email").val()) should do what you want.

    0 讨论(0)
  • 2020-12-24 00:23

    you can simply do this

    $('#mail').text($('#email').val())
    
    0 讨论(0)
  • 2020-12-24 00:28

    use keyup and change both.

    $("#boxx").on('keypress change', function(event) {
           var data=$(this).val();
           $("div").text(data);
    });
    

    here is the example http://jsfiddle.net/6HmxM/785/

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