簡體   English   中英

在javascript中將小寫字母轉換為大寫

[英]Convert lowercase letter to upper case in javascript

我有一個代碼 ,將小寫字母轉換為大寫但它只適用於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>

即使我嘗試過

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

onBlur文本框的事件

我該怎么做才能讓它在所有瀏覽器中都能運行?

謝謝

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

從您的HTML中分離javascript

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

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

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

如果文本框位於命名容器內,則使用類似這樣的內容

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

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

如果您不想創建一個顯式的JavaScript函數,可以在這里只執行一行:

分別轉換為大寫和小寫:

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

你可以簡單地使用CSS並進行text-transform:uppercase ,然后提交你運行toUppercase() 或者你只是提交混合,你在服務器端大寫字母:)

我想說最簡單的方法是......

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

你試過這個嗎?

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

謝謝...希望你喜歡它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM