如何从键Code获取字符串/字符值; [英] how to get String/character value from key Code;

查看:245
本文介绍了如何从键Code获取字符串/字符值;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用html的输入标签,该标签的工作方式应类似于Google搜索的输入.

I am using an input tag of html which is supposed to work similar to input of google search.

function onKeyPressFun(event){
    var inputObj = document.getElementById("tagInput");
    var strTags = inputObj.value + String.fromCharCode(event.keyCode);
    alert("post data: strTags" + strTags);
}

我想将数据发送到服务器以检测是否存在任何以该子字符串开头的标签.
我想将keyPress事件中的字符/字符串(在这里可以使用)添加到文本中的现有输入中.
就像上面的代码一样,我使用了String.fromCharCode.当我按'b'时,我发现keyCode 0,而String.fromCharCode(event.keyCode)的值什么也没有;也不是空白.
对此有任何解决办法吗?

I want to send the data to server to detect if any tag starting with this substring exist.
I want to add the character/string (whichever works here) from keyPress event to the existing input in the text.
As in the code above I used String.fromCharCode. When I press 'b' I found keyCode 0 and the value of String.fromCharCode(event.keyCode) was nothing; neither a blank.
Any fix for this?

推荐答案

它可能与不同浏览器中对.keyCode属性的支持有关.规范化keyCode值以获得更好的结果.一种方法(括号中的部分来自jQuery来源):

It may have something to do with support for .keyCode property across different browsers. Normalize keyCode value to get better results. One way to do it (part in parentheses is from jQuery source):

// e.g.
String.fromCharCode(event.charCode != null ? event.charCode : event.keyCode);

这篇关于如何从键Code获取字符串/字符值;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆