我正在使用 html 的输入标签,它应该与谷歌搜索的输入类似。
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) 的值什么都没有;既不是空白。
有什么解决办法吗?
最佳答案
这可能与不同浏览器对 .keyCode
属性的支持有关。规范化 keyCode 值以获得更好的结果。一种方法(括号中的部分来自 jQuery 源代码):
// e.g.
String.fromCharCode(event.charCode != null ? event.charCode : event.keyCode);
关于javascript - 如何从键代码中获取字符串/字符值;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12258929/