首页 > 解决方案 > 按键在我为某些聊天应用程序制作的功能中不起作用

问题描述

在我的函数中调用按键时,我的按键有一些问题,即使我使用多种解决方案,它也根本不起作用,所以我尝试调用不同的方法 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('');
    }
  });
)

它没有显示错误方法,但它也不适用于其他按键。

标签: javascriptasp.net-mvcsignalr

解决方案


正如@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('');
    }
  });
)

推荐阅读