2017-04-04 82 views
0

我有以下脚本可以将我在一个字段中输入的内容复制到另一个字段中。剥离空格并使用Javascript使字符串小写

DEMO:https://jsfiddle.net/wk3nmc76/

我在想,如果有可能,我可以改变第二场脱光空间&使价值小写?

<p><input type="text" name="full_name" id="full_name" placeholder="Full Name"/></p> 
<p><input type="text" name="last_name" id="last_name"></p> 
$('#full_name').keyup(function(){ 
    $('#last_name').val(this.value); 
}); 
+3

'$( '#姓氏')VAL(this.value.toLowerCase()代替。 (//g,''));'https://jsfiddle.net/satpalsingh/d2uyw659/ – Satpal

+0

Phenomenal,@Satpal - 非常感谢:-) – michaelmcgurk

回答

6

要做到这一点,你可以使用toLowerCase()组合和正则表达式来删除所有的空间。试试这个:

$('#full_name').keyup(function() { 
 
    $('#last_name').val(this.value.toLowerCase().replace(/\s/g, '')); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p> 
 
    <input type="text" name="full_name" id="full_name" placeholder="Full Name" /> 
 
</p> 
 

 
<p> 
 
    <input type="text" name="last_name" id="last_name"> 
 
</p>

+1

http://stackoverflow.com/questions/1983648/replace-space -with-划线和化妆的全字母 - 使用小写,-javascri pt是一个相当不错的副本。 – mplungjan

+0

非常感谢,罗里 - 这工作出色。真的很方便了解未来的工作。 – michaelmcgurk

1
$('#full_name').keyup(function(){ 
    $val = $(this).val() 
    stripped_val = $val.replace(' ', ''); 
    lowercase_stripped_val = stripped_val.toLowerCase(); 
    $('#last_name').val(lowercase_stripped_val); 
}); 

或简称:

$('#full_name').keyup(function(){ 
     $val = $(this).val() 
     modified_val = $val.replace(' ', '').toLowerCase(); 
     $('#last_name').val(modified_val); 
});