强制将输入元素的语言切换为英语 [英] force switch language to english for an input element
问题描述
好的,这是东西: 我只有一个要素
OK, here's the stuff: I got one element
<input type="text" id="username" size="15">
,当用户在此元素中键入内容时,我需要此元素来强制将键盘布局从任何其他语言切换为英语.我不需要将其发送到服务器.我只需要在输入语言时强制切换语言即可.
and I need this element to force switch keyboard layout from ANY other language to English when users type something in this element. I DO NOT need to send it to the server. I simply need to force switch the language while typing in it.
有人为它提供一个简单的JS解决方案吗?
Does anyone have a simple JS solution for it?
这就是我一直在想的: 从理论上讲.如果我知道应该切换的语言,那么我可以编写一个JS,将每个符号替换为英语
Here's what I've been thinking about: Theoretically. If i knew the language it shoud switch from, then i could write a JS which would replace every symbol to English
推荐答案
您是要只允许字母数字字符吗?如果是这样,您可以做这样的事情.
Do you just mean to only allow alphanumeric characters? If so, you could do something like this.
$('#username').keyup(function() {
if (this.value.match(/[^a-zA-Z0-9 ]/g)) {
this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
}
});
至于实际更改键盘布局本身,我不认为可以通过javascript ...
As for actually changing the keyboard layout itself, I don't believe that would be possible through javascript...
这篇关于强制将输入元素的语言切换为英语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!