Jquery:将一个文本输入镜像到另一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2977428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:39:40  来源:igfitidea点击:

Jquery: Mirror one text input to another

jquery

提问by atwellpub

I'm wanting to have one text field autopopulate with whatever the other text field has being typed into it.

我想让一个文本字段自动填充其他文本字段输入的内容。

How do I go about this?

我该怎么做?

Thank you! Hudson

谢谢!哈德逊

回答by Felix Kling

Short and simple:

简短而简单:

$('#idfield1').keypress(function() {
    $('#idfield2').val($(this).val());
});

or to bind it to several events in order to update it also if it loses focus:

或者将它绑定到多个事件以便在它失去焦点时也更新它:

$('#idfield1').bind('keypress keyup blur', function() {
    $('#idfield2').val($(this).val());
});

Reference: .keypress(), .val(), .blur(), .bind()

参考:.keypress(), .val(), .blur(),.bind()

Update:

更新:

Due to, for me, mysterious reasons, when the firstcharacter is typed in, it is not set in the other input field. Has anyone any idea? ;)

由于,对我来说,神秘的原因,当输入第一个字符时,它没有在另一个输入字段中设置。有人知道吗?;)

It works though by using keyup(keydownalso produces the same strange result).

它虽然通过使用keyup(keydown也会产生相同的奇怪结果) 来工作。

回答by naveen

Dustin Diaz has written a wonderful mirror for Twitter Widget using jQuery

Dustin Diaz 使用 jQuery 为 Twitter Widget 编写了一个精彩的镜像

$.fn.mirror = function (selector) {
    return this.each(function () {
        var $this = $(this);
        var $selector = $(selector);
        $this.bind('keyup', function () {
            $selector.val(($this.val()));
        });
    });
};

Use it like

使用它就像

$('#source_text_id').mirror('#destination_text_id'); 

回答by Mark Schultheiss

For an alternative:

对于替代方案:

$('#idfield1').bind('keyup keypress blur', function() 
{  
      $('#idfield2')[0].value = $(this)[0].value; 
});

or

或者

$('#idfield1').bind('keyup keypress blur', function() 
{  
    $('#idfield2').val($(this).val()); 
});

For more recent version of jQuery you can also use .on

对于更新版本的 jQuery,您还可以使用 .on

$('#idfield1').on('keyup keypress blur', function() 
{  
      $('#idfield2').val($(this).val());  
});

回答by Brynner Ferreira

Simple: http://jsfiddle.net/brynner/KnXJc/

简单:http: //jsfiddle.net/brynner/KnXJc/

HTML

HTML

<input type="text" class="mirror" placeholder="one">
<input type="text" class="mirror" placeholder="two">

jQuery

jQuery

$('.mirror').on('keyup', function() {
    $('.'+$(this).attr('class')).val($(this).val());
});

回答by Prabhat Gautam

$( document ).ready(function() {

    console.log( "ready!" );

   /*  $( ".checking" ).blur(function() {
          alert( "Handler for .blur() called." );
        }); */
        $(".formula_open").click(function(){
            getval();
         function getval(){
        var schedulenameexpression= $('.idfield1').val();
        var res = schedulenameexpression.split(/[\s()+*-/]+/);
        var i,text,substri,newitem;

        for (i = 0; i < res.length; i++) {
            text = res[i];
           substri = text.substr(6);
            var name ="schid_"+substri;
            var schname = $('#'+substri).val();
            console.log( schname );
            schedulenameexpression = schedulenameexpression.replace("schid_"+substri, $("#"+substri).val());
        }
         $('.idfield2').val(schedulenameexpression);
        }

    $('.idfield1').bind('keypress keyup blur propertychange', function() {
        getval();

});
});
});