2013-04-12 172 views
-4

在ASP.NET中使用JavaScript时,我需要将小写字母转换为大写字母。第一个字母大写

如何在JavaScript中使用正确答案解决此问题?

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
<title></title> 

<script language="JAVASCRIPT"> 
    function capitalizeMe(obj) { 
     val = obj.value; 
      newVal = ''; 
     val = val.split(' '); 
     for (var c = 0; c < val.length; c++) { 
      newVal += val[c].substring(0, 1).toUpperCase() + 
      val[c].substring(1, val[c].length) + ' '; 
     } 
    } 
</script> 

</head> 
<body> 
    <form id="Form2" runat="server"> 
     <asp:TextBox ID="TextBox1" runat="server" onblur="capitalizeMe(this)"> 
     </asp:TextBox> 
     <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
    </form> 
    </body> 
</html> 
+0

请之前发布SO上的搜索:http://stackoverflow.com/questions/1026069/capitalize-the-first-letter的dupplicate -of-string-in-javascript –

回答

0
<script language="JAVASCRIPT"> 
function capitalizeMe(obj) { 
    val = obj.value; 
     newVal = ''; 
    val = val.split(' '); 
    for (var c = 0; c < val.length; c++) { 
    newVal += val[c].substring(0, 1).toUpperCase() + 
val[c].substring(1, val[c].length) + ' '; 
    } 
    obj.value = newVal; 
    } 
    </script> 
3

你为什么不使用CSS代替?

.capitalized{ 
    text-transform: capitalize 
} 

类添加到您的元素,并有CSS处理其余部分

+0

我喜欢这种CSS方法。 – Daft

相关问题