send data with ajax when pressing enter

前端 未结 5 1215
无人共我
无人共我 2021-01-16 16:59

i have a form like this :

5条回答
  •  感情败类
    2021-01-16 17:28

    Move out SendData from document.ready

    function SendData() {
            $.ajax({
                url: "save.php",
                type: "post",
                data: "msg="+$('#chat_in').val(),
                dataType: 'json', 
                success: function(){   
                    alert("Sent");
                },
                error:function(){
                    alert("failure");                
                }
            });
        }
    
    $(document).ready(function () {
    
        $('#chat_in').keypress(function(e) {
            if(e.which == 13) {
                alert('You pressed enter!');
                SendData();
            }
        });
    
        $('#bt').click(function() {
            SendData();
        });
    });
    

提交回复
热议问题