Copy text of a field into another automatically

后端 未结 8 1174
攒了一身酷
攒了一身酷 2020-12-16 08:32

I need to copy the text entered in a field (whether it was typed in, pasted or from browser auto-filler) and paste it in another field either at the same time or as soon as

相关标签:
8条回答
  • 2020-12-16 08:51

    If you are using jQuery, it is very easy - you need just register the right function on the right event :)

    Here's the code:

    <input id="foo" />
    <input id="bar" />
    
    $(function(){
        var $foo = $('#foo');
        var $bar = $('#bar');
        function onChange() {
            $bar.val($foo.val());
        };
        $('#foo')
            .change(onChange)
            .keyup(onChange);
    });
    

    JSFiddle: http://jsfiddle.net/6khr8e2b/

    0 讨论(0)
  • 2020-12-16 08:55

    Try something like:

    $(document).ready(function () {
        $('#field_1').on('change', function (e) {
           $('#field_2').val($('#field_1').val());
        });
    });
    

    Heres a fiddle: http://jsfiddle.net/otwk92gp/

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