2010-11-16 241 views
1

表单提交的是正常请求而不是Ajax。Ajax表单提交

$(document).ready(function() { 

    StatusComments(); 

}); 


function StatusComments() { 

    $('.comment').submit(function() { 
     $(this).ajaxSubmit(options); 
     return false; 
    }); 

    var options = { 
     beforeSubmit: showRequest, 
     success: showResponse, 
     resetForm: true 
    }; 

    function showRequest(formData, jqForm, options) { 
     var textbox = $('#StatusMessageReplyMessage').val(); 
     alert(textbox); 

    } 

    function showResponse(responseText, statusText, xhr, $form) { 

    } 

} 

我有一个类似的状态更新这样

function StatusUpdates() { 
    $('#updateStatus').submit(function() { 
     $(this).ajaxSubmit(options); 
     return false; // prevent a new request 
    }); 

    var options = { 
     target: '.user-status', 
     // target element(s) to be updated with server response 
     beforeSubmit: showRequest, 
     // pre-submit callback 
     success: showResponse, 
     // post-submit callback 
     // other available options: 
     //url:  url   // override for form's 'action' attribute 
     //type:  type  // 'get' or 'post', override for form's 'method' attribute 
     //dataType: null  // 'xml', 'script', or 'json' (expected server response type) 
     //clearForm: true  // clear all form fields after successful submit 
     resetForm: true // reset the form after successful submit 
     // $.ajax options can be used here too, for example: 
     //timeout: 3000 
    }; 

    function showRequest(formData, jqForm, options) { 

     var textbox = $('#StatusMessageMessage').val(); 
     if ((textbox == '') || (textbox == "What have you been eating ?")) { 
      alert('Please Enter Something and click submit.'); 
      return false; 
     } else { 
      $('#StatusMessageMessage').attr('disabled', true); 
     } 

    } 

    function showResponse(responseText, statusText, xhr, $form) { 
     $('#StatusMessageMessage').attr('disabled', false); 
     $('.share').slideUp("fast"); 
     $('#StatusMessageMessage').animate({ 
      "height": "18px" 
     }, "fast"); 
    } 

} 
+0

你肯定没有JavaScript错误?你有没有检查你的错误控制台? – 2010-11-16 18:36:21

+0

这是否发生在所有浏览器?这可能是因为您的任何**的JavaScript中存在错误,导致脚本失败,从而无法发送AJAX。 – Trip 2010-11-16 18:47:35

+0

不,我没有得到任何错误。具有类似代码的状态消息部分起作用。这不是:( – 2010-11-16 19:08:16

回答

2

而不是

$('.comment').submit(function() { 
     $(this).ajaxSubmit(options); 
     return false; 
    }); 

尝试

$('.comment').click(function(e) { 
     e.preventDefault(); 
     $(this).ajaxSubmit(options); 
     return false; 
    }); 
+0

这使得一个ajax调用,但它会提交一个不同的URL比提交表单 – 2010-11-16 19:11:14

+0

该死的。我是单击按钮而不是形式。所以我没有工作:(。非常感谢 – 2010-11-17 05:05:52