【问题标题】:How to save the value of an input for a function?如何保存函数的输入值?
【发布时间】:2019-08-31 15:21:32
【问题描述】:

我是 PHP 和 SQL 以及 JQUERY/JS 的新手。我试图做到的是,当一个人按下回车键(在输入字段中)时,就会发生同样的事情,就好像他们按下了现有的按钮一样。

这是原始按钮代码:

$(document).on("click", '.submit', function(event) { 
  var to_user_id = $(this).attr('id');
  to_user_id = to_user_id.replace(/chatButton/g, "");
  sendMessage(to_user_id);
});

我尝试让 enter 和上面的代码做同样的事情:

$('#chatMessage3').keyup(function(e) {  
  if (e.keyCode == 13) {   
    var to_user_id = $(this).attr('id');

  sendMessage(to_user_id);  
}

【问题讨论】:

  • 问题,你有吗?
  • 看起来你是这样做的,除了to_user_id = to_user_id.replace(/chatButton/g, ""); 行。
  • 而且我认为$('#chatMessage3') 必须具有id 属性,这与.submit 相同。

标签: javascript php jquery mysql sql


【解决方案1】:

看起来您的提交按钮的 ID 为 chatButton1chatButton2 等,而您只想发送 12。您需要在 #chatMessage3 处理程序中进行类似的替换:

$('#chatMessage3').keyup(function(e) {  
  if (e.keyCode == 13) {   
    var to_user_id = $(this).attr('id');
    to_user_id = to_user_id.replace(/chatMessage/, '');
    sendMessage(to_user_id);  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多