jQuery:将一个文本输入镜像到另一个文本 [英] Jquery: Mirror one text input to another

查看:86
本文介绍了jQuery:将一个文本输入镜像到另一个文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让一个文本字段自动填充其中已键入的其他文本字段.

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

我该怎么办?

谢谢! 哈德逊

推荐答案

又简单又简单:

$('#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());
});

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

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

更新:

由于我的神秘原因,在键入 first 字符时,未在其他输入字段中进行设置.有谁知道吗? ;)

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

尽管通过使用keyup也可以工作(keydown也会产生相同的奇怪结果).

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

这篇关于jQuery:将一个文本输入镜像到另一个文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆