文本框中输入小写字母即时转换为大写

在系统中,有一个文本框,要求输入大写字母。但是用户不自觉,只好在程序来控制了。

在网页中,拉一个TextBox控件:

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


写Javascript脚本,可使用onkeyup事件,即时把字母转换为大写字母:

View Code
<script type="text/javascript">
                                window.onload = function () {
                                    var textBox = document.getElementById("<%= TextBoxSeriesNumber.ClientID %>");

                                    textBox.onkeyup = function () {
                                        this.value = this.value.toUpperCase();
                                    };
                                };
                            </script>


Demo:




以下内容于15:08分补充:
上面的方法,会有一个问题,就是先显示小写字母,再转变为大写字母。在网上查找其它资料时,又无意中发有一个更好的方法,就是使用CSS来实现:

style="text-transform:uppercase;"


完整应用如下:


有关text-transform样式属性说明:

 

 

posted @ 2013-03-28 11:33  Insus.NET  阅读(1219)  评论(2编辑  收藏  举报