简体   繁体   English

从小写到大写

[英]From lowercase to upper case

hey, I trying to enter the text in lowercase letter but when it comes to textbox it must be in uppercase if anyone can do this.嘿,我试图以小写字母输入文本,但是当涉及到文本框时,如果有人可以这样做,它必须是大写字母。

You can do this with CSS, no need for JavaScript or PHP您可以使用 CSS 执行此操作,不需要 JavaScript 或 PHP

text-transform:uppercase
text-transform:lowercase

Add these to the style of each element class.将这些添加到每个元素 class 的样式中。

Or if you are resorting to inline styling, do this.或者,如果您使用内联样式,请执行此操作。

<input type="text" style="text-transform:uppercase;" />

String to uppercase with PHP ( php.net doc ):使用 PHP ( php.net doc ) 将字符串转为大写:

strtoupper("Hello")

Output: HELLO

String to uppercase with JavaScript ( java2s.com ):使用 JavaScript ( java2s.com ) 将字符串转为大写:

var s = new String("Hello")
s.toUpperCase()

Output: HELLO

Your options are:您的选择是:

  1. As Raoul said , use CSS. 正如 Raoul 所说,使用 CSS。 If you possibly can, this is your best bet.如果可以的话,这是你最好的选择。
  2. Let the user type in lower case, then transform it when they leave the field ( blur , etc.) or when you save/process the text (eg, on the server, whatever).让用户输入小写,然后在他们离开字段( blur等)或保存/处理文本时(例如,在服务器上等)进行转换。 Here's a blur example:这是一个blur的例子:

     document.getElementById('theIDOfTheTextArea').onblur = function() { this.value = this.value.toUpperCase(); };

    Live example活生生的例子

  3. Do it with JavaScript in the keydown or keypress event.keydownkeypress事件中使用 JavaScript 执行此操作。 Despite the various attempts intermittently posted here, doing this with JavaScript not trivial .尽管在这里间歇性地发布了各种尝试,但使用 JavaScript 执行此操作并非易事 It's trivial to identify the keypresses you want to handle and to cancel the event to prevent that keypress being added;识别您要处理的按键并取消事件以防止添加该按键是微不足道的; but then inserting the character you do want is non-trivial.但随后插入想要的字符并非易事。 (Sadly, the keyboard events don't just let you substitute a different character; that would be nice, but they don't.) It unfortunately requires that you use text ranges / selections and gets you into areas that vary cross-browser. (遗憾的是,键盘事件只是让您替换一个不同的字符;这很好,但他们没有。)不幸的是,它要求您使用文本范围/选择并让您进入跨浏览器不同的区域。 You'd probably need to leverage a library like Rangy to do it.您可能需要利用像Rangy这样的库来做到这一点。 I'd Just Say No and go the CSS / post-processing route.我只想说不,go CSS / 后处理路线。

The answer to this post will solve your problem. 这篇文章的答案将解决您的问题。

  • Get the textbox element by id通过id获取文本框元素
  • Set an onBlur listener and convert its value using string.toUpperCase()设置一个 onBlur 监听器并使用 string.toUpperCase() 转换它的值

Have you check this你检查过这个吗

Upper

$("#idoftextbox").blur(function(){$(this).val($(this).val().toUpperCase())});

Put this in an onload event把它放在一个 onload 事件中

Easy:简单的:

textareaElement.onkeydown = function(e) {
    //make sure IE gets it
    e = e || event;
    //don't insert
    e.preventDefault();
    //catch the ASCII key code, and convert it to uppercase letter
    this.value += String.fromCharCode(e.keyCode).toUpperCase();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM