1

我想知道是否可以在 jQuery 键盘事件中检测重音(á、ő、ű、ö 等)。

先感谢您。

4

3 回答 3

1

尝试使用正则表达式...

$('#input').on('keyup', function(){
    var myRegex = /[^a-zA-Z0-9]/;
    var text = $(this).val();
    if(myRegex.test(text)){
        alert('accent detected');
    }
});

请参阅此jsFiddle 示例

问候...

于 2013-01-29T15:36:34.950 回答
0

在按键事件中,您可以使用此 ASCII 表http://www.asciitable.com/index/extend.gif查看字符的 ASCII 码

于 2013-01-29T15:36:00.537 回答
0

对不起@MG_Bautista,但你的答案是错误的。在您的示例中,如果您尝试使用一些字符,例如!,?,它会说“检测到重音”。但是@sada 只需要重音字符您必须更改正则表达式:

var MyRegex = /[^a-zA-Z0-9!"'\.,{}\[\]\(\)\\=|°#$%&/?¿¡´+*-_]/;

也许我忘记了一些“特殊”角色。因此,真正的解决方案需要仅检测您需要的字符而不是不需要的字符。

var MyRegex = /[áéíóúÁÉÍÓÚâêîôû]/; //and all the ones you need to detect
if (MyRegex.test(text)){
    alert("accent detected");
}

Important: If you need to test every character, you have to iterate over the string.

I hope this helps, Bye.

于 2013-07-04T14:15:27.587 回答