0

使用成功和错误时如何停止使用 AJAX 刷新的表单?如果我删除返回 false;函数结束后,显示错误(验证 CLid 时出错!)并刷新页面,但是如果我离开它,ajax 函数将提交并返回 true 而不刷新。如果您需要更多信息,请与我们联系。谢谢!


var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&company=' + phone + '&length=' + length + '$time=' + time + '&question' + question;

$.ajax({  
    type: "POST",  
    url: "contact_process.php",  
    data: dataString,  
    success: function() {  
       $('#search_contact').html("You're request for more information was submitted."); 
    },
    error: function() {
      $('#search_contact').html('Error validating CLid!');          
    } 
}); 


return false;

});
    
4

1 回答 1

1

I suspect you are triggering this ajax request through an event on a form submission or link click or something.

If you are triggering the request from a link, ie:

<a href="contact_process.php" id="contactTrigger">Click me</a>

When you bind the event you need to call event.preventDefault(); on the event passed to the event handler to prevent the default behavior, ie:


$('#contactTrigger').click(function(event){
   event.preventDefault();
   //do your ajax stuff here.
});
于 2013-07-25T01:07:51.320 回答