我是PHP和SQL和JQUERY / JS的新手。我想做的是,当有人按下Enter键(在输入字段中)时,会发生相同的事情,就像他们按下现有按钮一样。
这是原始的按钮代码:
$(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);
});
我尝试使之进入与上述代码相同的事情:
$('#chatMessage3').keyup(function(e) {
if (e.keyCode == 13) {
var to_user_id = $(this).attr('id');
sendMessage(to_user_id);
}
答案 0 :(得分:1)
您的提交按钮似乎具有chatButton1
,chatButton2
等ID,而您只想发送1
,2
。您需要在#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);
}