如何使用jQuery .keyup()函数捕获键盘输入

编程入门 行业动态 更新时间:2024-10-22 22:52:40
本文介绍了如何使用jQuery .keyup()函数捕获键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

处理一个简单的刽子手游戏,我试图用 keyup()来捕获用户输入,但是当我将它记录到控制台时我就是意识到某事不正常......这是我的代码:

Working on a simple hangman game, and I'm trying to catch the user input with the keyup(), but when I log it to the console I'm realizing something isn't working right... this is my code:

$(document).keyup(function(e) { userInput = e.value; console.log(userInput); });

我也试过这样做,但它也没有用。

I also tried doing this, but it isn't working either.

$(document).keyup(function(e) { userInput = $(this).val(); console.log(userInput); });

哦,顺便说一下, userInput是一个全局变量。

推荐答案

您必须从目标中获取值 活动的属性。请尝试以下方式:

You have to get the value from target property of the event. Try the following way:

$('#txtInput').on('keyup', function(e) { var userInput = e.target.value; console.log(userInput); });

<script src="ajax.googleapis/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Input: <input type='text' id="txtInput"/>

更多推荐

如何使用jQuery .keyup()函数捕获键盘输入

本文发布于:2023-11-07 05:27:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565662.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   函数   键盘输入   keyup   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!