JQuery form submit with function not working

对着背影说爱祢 提交于 2019-12-02 02:49:35

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//
         }
         });
       }
    }); 
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!