2016-10-01 17 views
-1

こんにちは私は入力ボタンをクリックすると送信ボタンをトリガーするajaxチャットを使用しています。入力ボタンを1回クリックすると何も問題はありません。ボタンを繰り返し呼び出しを提出し、チャットメッセージは、この問題 サンプルjqueryのコードで複数のtimes.pleaseヘルプを保存:チャットで長い入力ボタンの問題を防ぐ方法

$("#chat_message").keypress(function(event) { 
     if (event.which == 13) { 
      event.preventDefault(); 
      $("#send-btn").click(); 
      return false; 
     } 
    }); 

htmlコード:

<input type="text" name="message" id="chat_message" > 
<button type="button" id="send-btn">Send</button> 
+0

はまた、代わりにkeyUpイベントに '$( "#のchat_message")' – Rayon

+1

使用の値をチェックしましたkeypress – cheralathan

+0

@cheralathanこのシナリオではキーアップが適用されないHTMLコードを確認してください –

答えて

0

おかげで、私は、ソリューション、

$("#chat_message").keypress(function(event) { 
    if (event.which == 13) { 
    event.preventDefault(); 
     if ($('#chat_message').val() != "") { 
     $("#send-btn").click(); 
     $('#chat_message').val(""); 
    } 
    return false; 
    } 
    }); 
1

はまあkeyupイベントがあなたのケースで完璧に動作します。

以下のコードをご覧ください。返信用

$(document).ready(function(){ 
 
    
 
    //the keyup event 
 
    $("#chat_message").keyup(function(event) { 
 
     if (event.which == 13) { 
 
      event.preventDefault(); 
 
      $("#send-btn").click(); 
 

 
      return false; 
 
     } 
 
    }); 
 
    
 
    $("#send-btn").on("click",function(){ 
 
    console.log($("#chat_message").val()); 
 
    }); 
 
});
<html> 
 
    <head> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
    </head> 
 
    <body> 
 
    <input type="text" name="message" id="chat_message" > 
 
    <button type="button" id="send-btn">Send</button> 
 
</body> 
 
</html>

関連する問題