跟踪用户键在单独函数中输入字段中的按下
我想在单独的函数中跟踪输入字段中的用户键,并根据onkeydown
值调用功能。我下面的东西不起作用。什么都没有发生。我愿意睁大眼睛启示和评论。我想我错过了一些专业的东西。
function find_key(Fval, Fid) {
//prevents malicious characters
if ((event.charCode <= 47) || (event.charCode >= 64)) {
return;
};
// user presses ctrl + delete
// this doesn't work, nothing happens
if (event.code == 46 && (event.ctrlKey || event.metaKey)) {
alert('Undo!');
}
// user presses ctrl + enter
// this doesn't work, nothing happens
if (event.ctrlKey && e.keyCode == 13) {
alert("grab");
grabName(Fval, Fid);
}
}
<input onkeydown="find_key(this,this.id);" id="P${i}" />
我根据建议尝试了此操作:(它不起作用)
`<input class = 'tiger' id = "P${i}" />`;
document.getElementsByClassName('Tiger')。AddeventListener('KeyPress',findKey);
function find_key (e) {
if (event.code == 46 && (event.ctrlKey || event.metaKey))
{ alert('Undo!'); }
I want to track user key presses in input field in a separate function and call a function based on onkeydown
value. What I have below just doesn't work. Nothing happens. I'm open to eye-opening revelations and commentary. I think I'm missing something major.
function find_key(Fval, Fid) {
//prevents malicious characters
if ((event.charCode <= 47) || (event.charCode >= 64)) {
return;
};
// user presses ctrl + delete
// this doesn't work, nothing happens
if (event.code == 46 && (event.ctrlKey || event.metaKey)) {
alert('Undo!');
}
// user presses ctrl + enter
// this doesn't work, nothing happens
if (event.ctrlKey && e.keyCode == 13) {
alert("grab");
grabName(Fval, Fid);
}
}
<input onkeydown="find_key(this,this.id);" id="P${i}" />
I tried this based on a suggestion: (it doesn't work, though)
`<input class = 'tiger' id = "P${i}" />`;
document.getElementsByClassName('tiger').addEventListener('keypress', findKey);
function find_key (e) {
if (event.code == 46 && (event.ctrlKey || event.metaKey))
{ alert('Undo!'); }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将事件作为参数传递(请注意必须使用确切的参数名称“事件”)。
然后在JS
P.S.为什么不在JS代码中使用文档方法并将事件侦听器直接添加到您的输入中?
You need to pass event as an argument (please note exact argument name "event" must be used).
And then in js
P.S. why not using document method in js code and adding event listener to your input directly?