且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

键入时将文本框内容复制到另一个文本框

更新时间:2023-11-27 18:14:52

尝试使用此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/