2016-06-18 41 views
-3

我需要字符串进行转换,首字母大写,其余全部小写,存储在我的数据库中。Javascript - 首字母大写,其余小写

例如,用户输入

name="john"; 

,我需要它改造成

name="John"; 
+1

这已被问及,无需提供重复的问答。 – nicael

+0

不完全重复,因为OP要求将剩余部分缩小 - 链接问题仅限于大写第一个字符。显然,这两个都是简单的问题,但不是重复的。虽然这个问题需要澄清。 –

+0

@MrUber请澄清一下您的问题:您的意思是“存储在我的数据库中”是什么意思?您是否在客户端(可以随时避开)或服务器端寻找“活的”高级别低端服务器? –

回答

0

这是一个简单的脚本,可以让你做到这一点。

<input type="text" id="name" onblur="capitalize(this.id)" value="" autofocus> 

<script> 
    function capitalize(id) 
    { 
     var text = document.getElementById(id).value; //get the text 
     var firstL = text.slice(0,1).toUpperCase(); //get the first character and make it Uppercase 
     var rest = text.slice(1).toLowerCase(); //get the rest of the string and make it Lowercase 
     var text = firstL.concat(rest); //create the new text store it 
     document.getElementById(id).value = text; //set the value of the field 
    } 
</script> 

onblur事件可能与不同的事件,如onkeyup更改为更立竿见影的效果。

+0

为什么downvote ??? –