jquery ajax提交问题

时间:2010-08-23 13:35:12

标签: jquery

我在以下方面遇到了一些麻烦:

<script>
$(document).ready(function() {
$('#sform').validate({
rules: { firstName: {required:true},
         lastName:  {required:true},
         emailAddress:  {required:true, email:true}
},
messages: { firstName: '<br/><span style="color:red;">Please enter your first name!</span>',
            lastName : '<br/><span style="color:red;">Please enter your last name!</span>',
            emailAddress: '<br/><span style="color:red;">Please enter a valid email address!</span>'
},
   submitHandler: function(form){
   var options = { 
                beforeSubmit:  showRequest,
                success: showResponse,
                type: 'post',
                url:'sendSurvey.cfm'
                };
    $(this).ajaxSubmit(options);
    return false;   
   }
 });
function showRequest(formData, jqForm, options) { 
 $('#formSub').html('We really appreciate your feedback!');
var queryString = $.param(formData); 
 alert('About to submit: \n\n' + queryString); 
  return true; 
 }
function showResponse(responseText, statusText) {
  alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
       '\n\nThe output div should have already been updated with the responseText.'); 
 }
 });
 </script>

问题是没有发布表单数据。$ .param(formData)为null。该怎么办?感谢

1 个答案:

答案 0 :(得分:1)

您需要提交<form>,因此您的内容有点偏离,而不是:

$(this).ajaxSubmit(options);

你需要这个:

$(form).ajaxSubmit(options);

您在this中呼叫submitHandler的位置,this指的是验证对象本身,而不是<form>,所以只需使用form传入的DOM元素引用。