Javascript 在javascript中将小写字母转换为大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2354716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:54:52  来源:igfitidea点击:

Convert lowercase letter to upper case in javascript

javascript

提问by priyanka.bangalore

I have a codethat will convert lower case letters to uppercase but it works only with IE and not in Crome or Firefox.

我有一个代码可以将小写字母转换为大写字母,但它仅适用于 IE 而不适用于 Crome 或 Firefox。

function ChangeToUpper()
  {         
            key = window.event.which || window.event.keyCode;

            if ((key > 0x60) && (key < 0x7B))
            window.event.keyCode = key-0x20;
  }



<asp:TextBox ID="txtJobId" runat="server" MaxLength="10" onKeypress="ChangeToUpper();"></asp:TextBox>

Even I tried with

即使我试过

document.getElementById("txtJobId").value=document.getElementById("txtJobId").value.toUpperCase(); 

onBlur event of the textbox

文本框的 onBlur 事件

What should I do to make it work in all browsers?

我应该怎么做才能使它在所有浏览器中都能正常工作?

Thanks

谢谢

回答by rahul

<script type="text/javascript">
    function ChangeCase(elem)
    {
        elem.value = elem.value.toUpperCase();
    }
</script>
<input onblur="ChangeCase(this);" type="text" id="txt1" />

separate javascript from your HTML

将 javascript 与 HTML 分开

window.onload = function(){
        var textBx = document.getElementById ( "txt1" );

        textBx.onblur = function() {
            this.value = this.value.toUpperCase();
        };
    };

<asp:TextBox ID="txt1" runat="server"></asp:TextBox>

If the textbox is inside a naming container then use something like this

如果文本框在命名容器内,则使用类似这样的内容

var textBx = document.getElementById ("<%= txt1.ClientID %>");

            textBx.onblur = function() {
                this.value = this.value.toUpperCase();
            };)

回答by Kyle Rozendo

回答by Ionu? Staicu

You can simply use CSS and do text-transform:uppercaseand on submit you run toUppercase(). Or you just submit as mixed and you capitalize letters on server side :)

您可以简单地使用 CSS 并text-transform:uppercase在提交时运行toUppercase()。或者你只是混合提交,然后在服务器端将字母大写:)

回答by Muhammad Awais

If you don't want to make an explicit JavaScript function, here you can do it in just one line:

如果你不想创建一个显式的 JavaScript 函数,你可以在这里只用一行来完成:

Convert to lower and upper case respectively:

分别转换为小写和大写:

<asp:TextBox ID="txt1" onblur='this.value = this.value.toLowerCase();'></asp:TextBox>
<asp:TextBox ID="txt1" onblur='this.value = this.value.toUpperCase();'></asp:TextBox>

回答by Theek

I would say the easiest way is..

我会说最简单的方法是..

<input id="yourid" style="**text-transform: uppercase**" type="text" />

回答by Jude

Have you tried this?

你试过这个吗?

var myString = "this is a String";
alert(myString.toUpperCase()); // "THIS IS A STRING"
alert(myString.toLowerCase()); // "this is a string"

Thanks... Hope you like it.

谢谢...希望你喜欢它。