简体   繁体   中英

jQuery: Autofill input values and convert formatting

Using this simple code when you fill #input1 field, the #input2 input gets autofilled with same value:

$("#input1").keyup(function(){
    $("#input2").val(this.value);
});

So eg if you type in 'Foo Bar' into #input1 , it also gets filled as 'Foo Bar' in #input2 .

But how do I autofill it into #input2 and at same time eg convert to all lower case or all uppercase and with no spaces? So eg if #input1 contains 'Foo Bar' then #input2 autofills with 'FOOBAR' or 'foobar'?

http://jsfiddle.net/bxHQ5/1037/

您可以执行以下操作:

$("#input2").val($("#input1").value.toLocaleLowerCase().replace(/\s/g, ''));

Like this:

$("#input1").keyup(function(){
    $("#input2").val(this.value.toLocaleLowerCase());
});

Or for uppercase, like this:

$("#input1").keyup(function(){
    $("#input2").val(this.value.toLocaleUpperCase());
});

您可以调用函数ToLowerCasetoUpperCasereplace以退出空格,如下所示:

$("#input2").val(this.value.toLowerCase().replace(' ', ''));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM