JQuery form submit with function not working

前端 未结 1 1614
醉梦人生
醉梦人生 2021-01-23 14:31

I\'m having an issue with the jquery function submit for a form :

$(document).ready(function () {
    $(\'#message\').keydown(function(e) {
      if(e.which == 1         


        
相关标签:
1条回答
  • 2021-01-23 14:58

    I don't believe it will work the way you are trying to do it. When it's inside the submit function, the alert will never fire until it gets a response back from POST. Which means you need a response from your form processing script.

    Your AJAX call doesn't need to be inside the submit function, it just needs to be inside the event.

    $(document).ready(function () {
        $('#selfie_message').keydown(function(e) {
          if(e.which == 13 && !e.shiftKey) {
             $('#edit_selfie_11').submit();           
    
             $.ajax({
             type: "POST",
             url: "/selfies/11",
             data: $("#edit_selfie_11").serialize()
             });
           }
        }); 
    });
    

    If you need something to happen on success, you would do it like this.

    $(document).ready(function () {
        $('#selfie_message').keydown(function(e) {
          if(e.which == 13 && !e.shiftKey) {
             $('#edit_selfie_11').submit();           
    
             $.ajax({
             type: "POST",
             url: "/selfies/11",
             data: $("#edit_selfie_11").serialize(),
             success: function(response){
             //your response code here//
             }
             });
           }
        }); 
    });
    
    0 讨论(0)
提交回复
热议问题