I am using an input tag of html which is supposed to work similar to input of google search.
我正在使用html的输入标签,它应该与谷歌搜索的输入类似。
function onKeyPressFun(event){
var inputObj = document.getElementById("tagInput");
var strTags = inputObj.value + String.fromCharCode(event.keyCode);
alert("post data: strTags" + strTags);
}
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?
我想将数据发送到服务器以检测是否存在以此子字符串开头的任何标记。我想将keyPress事件中的字符/字符串(无论哪个在这里工作)添加到文本中的现有输入。在上面的代码中,我使用了String.fromCharCode。当我按'b'时,我发现keyCode为0,String.fromCharCode(event.keyCode)的值为空;不是空白。对此有任何修复?
2 个解决方案
#1
7
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):
它可能与跨不同浏览器的.keyCode属性的支持有关。规范化keyCode值以获得更好的结果。一种方法(括号中的部分来自jQuery源代码):
// e.g.
String.fromCharCode(event.charCode != null ? event.charCode : event.keyCode);
#2
1
You can try:
你可以试试:
String.fromCharCode(event.which); // it works in all major browsers
#1
7
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):
它可能与跨不同浏览器的.keyCode属性的支持有关。规范化keyCode值以获得更好的结果。一种方法(括号中的部分来自jQuery源代码):
// e.g.
String.fromCharCode(event.charCode != null ? event.charCode : event.keyCode);
#2
1
You can try:
你可以试试:
String.fromCharCode(event.which); // it works in all major browsers