問題描述
我使用 jquery 驗證插件在提交前驗證表單在 submitHandler 我使用 ajax 請求用 ajax 發布表單在我使用 .ajax 發送請求之前,但現在表單有圖像并且很難通過正常的ajax請求序列化文件元素,因此我使用了這個插件http://www.malsup.com/jquery/form/
i use the jquery validation plugin to validate form before submit in submitHandler i use ajax request to post the form with ajax before i used .ajax to send the request but now the form have image and its so hard to serialize the file element through the normal ajax request and because of this i used this plugin http://www.malsup.com/jquery/form/
現在使用插件后,ajax 請求無法正常工作,不知道為什么這是第一個使用普通 ajax 調用的示例
now after using the plugin the ajax request not working at it all don't know why this the first example with the normal ajax call
$(document).ready(function(){
$("#categoryForm").validate({
submitHandler: function() {
$.ajax({
type:'POST',
url: 'actions/add-category.php',
data: $("#categoryForm").serialize(),
success: function(response) {
$('#status').html(response);
}
});
return false;
}
});
});
使用插件后的這個
$(document).ready(function(){
$("#categoryForm").validate({
submitHandler: function() {
$('#categoryForm').ajaxForm(function() {
alert('the form was successfully processed');
});
return false;
}
});
});
第二個不工作
推薦答案
嘗試反轉函數:
jQuery('#form_id').ajaxForm({
beforeSubmit: function() {
jQuery("#form_id").validate({
rules: {
first_name: "required",
last_name: "required"
},
messages: {
first_name: "Required",
last_name: "Required"
}
});
return jQuery('#form_id').valid();
},
success: function(resp) {
jQuery('#resp').html(resp).fadeIn('fast');
}
});
其中 #resp
是將接收從您的 #form_id
發布的文件生成的 HTML 的 ID
Where #resp
is the ID that will receive the HTML generated from the file your #form_id
posted
這篇關于.ajaxform 在驗證 submitHandler 中不起作用?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!