これは機能します:
$('#contestform').on('submit', function(e) {
$.post( '/', $(this).serialize(), function(response){
console.log(response);
},'json' );
e.preventDefault();
});
そしてこれはうまくいきます
jQuery("#contestform").validate();
しかし、それらを連携させる方法がわかりません。送信がクリックされ、フォームが有効な場合にのみ、トップコードの内容が発生するようにします。http://docs.jquery.com/Plugins/Validation/validate#toptionsのものを試しましたが、わからないと思います。ある時点ではある程度機能していましたが、送信を2回クリックする必要がありました。
1 つのことは、私がそれを助けることができれば、2 つの部分が php の異なる部分から来るので、別の関数に post() のものを保持したいということです。
編集1 はこれを試しましたが、(フォームの下で)機能しませんでした(機能しないということは、ajaxではなく、正常に送信されていることを意味します)
function postForm(e) {
jQuery.post( '/', $(this).serialize(), function(response){
console.log(response);
},'json' );
e.preventDefault();
}
jQuery("#contestform").validate({submitHandler: postForm});
edit2 まあ、jQuery.Form プラグイン http://www.malsup.com/jquery/form/#ajaxSubmitで動作する ようになりました それでも、上記のコードで何が間違っていたのか知りたいです。
function showResponse(responseText, statusText, xhr, $form){
console.log(responseText);
}
var soptions = {
success: showResponse ,
dataType: 'json'
};
jQuery("#contestform").validate({
submitHandler: function(form) {
jQuery(form).ajaxSubmit(soptions);
return false; // always return false to prevent standard browser submit and page navigation
}
});