键入时将文本框内容复制到另一个文本框 [英] copy text box content to another textbox while typing

查看:166
本文介绍了键入时将文本框内容复制到另一个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了j查询,该查询将内容从一个文本框复制到另一个文本框. 不是我的代码中的j查询专家

I writing j query which copy content from one text box into another. am not an expert in j query following in mine code

    $(function() {
    $('input[id$=tb1]').keyup(function() {
        var txtClone = $(this).val();
        $('input[id$=txtCustName]').val(txtClone);
    });
});

推荐答案

尝试使用此js:

$('input[id$=tb1]').on('keyup',function() {
    $('input[id$=txtCustName]').val($(this).val());
});

使用jQuery的 on() 绑定到事件要好得多,而您不会必须先将val设置为变量...

use jQuery's on() to bind to the event is much better, and you don't have to set the val to a variable first...

编辑

如果您有类似html的代码,则上面的代码会将内容克隆到以txtCustName结尾的任何字段中

the above code will clone the content into any field ending with txtCustName if you have html like:

<input id="random_tb1"/>
<input id="text_txtCustName"/>
<input id="other_tb1"/>
<input id="stuff_txtCustName"/>

它不知道您要哪个,所以如果您将html做成这样:

it has no idea which one you want, so if you make your html something like this:

<div>
    <input id="random_tb1"/>
    <input id="text_txtCustName"/>
</div>
<div>
    <input id="other_tb1"/>
    <input id="stuff_txtCustName"/>
</div>

您可以将它们用html分隔,并仅使用此JS更新相关字段:

you can keep them separated in html, and only update the related field with this JS:

$(function() {

    $('input[id$=tb1]').on('keyup',function() {
        $('input[id$=txtCustName]',$(this).parent()).val($(this).val());
    });

});​

这是一个演示: http://jsfiddle.net/JKirchartz/XN2qD/

这篇关于键入时将文本框内容复制到另一个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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