简体   繁体   English

jQuery:自动填充输入值并转换格式

[英]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字段时,使用以下简单代码, #input2输入将自动填充为相同的值:

$("#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 . 因此,例如,如果您在#input1 “ Foo Bar”,它也会在#input2被填充为“ Foo Bar”。

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? 但是,如何将它自动填充到#input2 ,同时#input2转换为所有小写字母或全部大写字母且没有空格? So eg if #input1 contains 'Foo Bar' then #input2 autofills with 'FOOBAR' or 'foobar'? 因此,例如,如果#input1包含“ Foo Bar”,那么#input2自动填充“ FOOBAR”或“ foobar”?

http://jsfiddle.net/bxHQ5/1037/ 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(' ', ''));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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