javascript - 按键在我为某些聊天应用程序制作的功能中不起作用
问题描述
在我的函数中调用按键时,我的按键有一些问题,即使我使用多种解决方案,它也根本不起作用,所以我尝试调用不同的方法 key up 或 key down 但它也不起作用
chat.connection.start().done(function () {
var connectionname = prompt();
chat.server.setConnectionName(connectionname);
$("#myConnectionName").val(connectionname);
$("inmessage").keypress(function (e) {
if (e.which == 13) {
var partnerId = $(this).closest('#chatBox').find('#partnerConnectionId').val();
var message = $(this).val();
chat.server.send(partnerId, message);
$(this).val('');
}
});
)
它没有显示错误方法,但它也不适用于其他按键。
解决方案
正如@briosheje 在评论中提到的,您需要使用类或 id 选择器:
chat.connection.start().done(function () {
var connectionname = prompt();
chat.server.setConnectionName(connectionname);
$("#myConnectionName").val(connectionname);
$(".inmessage").keypress(function (e) {
if (e.which == 13) {
var partnerId = $(this).closest('#chatBox').find('#partnerConnectionId').val();
var message = $(this).val();
chat.server.send(partnerId, message);
$(this).val('');
}
});
)
推荐阅读
- javascript - 如何检查单选按钮选择值以匹配数组项
- ionic-framework - Ionic 4 相机屏幕按钮
- ruby - 实例变量未传递给块
- django - 无法使用 graphene_django 从 GraphQL 获取最后 n 个元素
- c - C: fork() - Child 和 Parent 没有按预期行事
- javascript - 如何在我的网站中阻止请求域
- android - BottomNavigationView 在滚动时隐藏,但在片段更改时不显示
- c# - 正则表达式捕获两个分隔符之间的文本,包括“共享”
- google-chrome - 在 Chrome DevTools 控制台设置中保留“仅选定的上下文”
- c# - Outlook 的电子邮件类型为 EX 时如何获取发件人的电子邮件?