jQuery表单提交功能不起作用 [英] JQuery form submit with function not working

查看:91
本文介绍了jQuery表单提交功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为表单提交jquery函数时遇到问题:

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

$(document).ready(function () {
    $('#message').keydown(function(e) {
      if(e.which == 13 && !e.shiftKey) {
         $('#edit_message_11').submit(function() {
            alert("HELLO2");            
         });            
         return false;
      }
    }); 
}); 


<form id="edit_message_11" class="edit_message" method="post" action="/message/11" accept-charset="UTF-8">
<textarea id="message" class="form-control edit_message_form" name="message">
Hello
</textarea>

http://jsfiddle.net/978QC/

当我对表单执行以下操作时: $('#edit_message_11').submit(function(){...}); 不会触发提交.

When I do the following for my form : $('#edit_message_11').submit(function() { ... }); it doesn't trigger the submit.

但是,如果我执行 $('#edit_message_11').submit(); ,则会触发提交.

However, If I do $('#edit_message_11').submit(); it does trigger the submit.

我需要做 $('#edit_message_11').submit(function(){...}); 的原因是因为我想进行ajax提交.

The reason why I need to do $('#edit_message_11').submit(function() { ... }); is because I want to do an ajax submit.

有人知道吗?

谢谢!

推荐答案

我认为它不会像您尝试的那样起作用.当它位于Submit函数中时,该警报将永远不会触发,直到它从POST返回响应为止.这意味着您需要表单处理脚本的响应.

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.

您的AJAX调用不需要在Submit函数之内,只需在事件之内即可.

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//
         }
         });
       }
    }); 
});

这篇关于jQuery表单提交功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆