jQuery验证:如何在表单有效后发布()?
这项工作:jQuery验证:如何在表单有效后发布()?,jquery,jquery-validate,Jquery,Jquery Validate,这项工作: $('#contestform').on('submit', function(e) { $.post( '/', $(this).serialize(), function(response){ console.log(response); },'json' ); e.preventDefault(); }); 这是有效的 jQuery("#contestform").validate(); 但我不知道如何让他们一起工作。我希望只有在点击提交并且表单
$('#contestform').on('submit', function(e) {
$.post( '/', $(this).serialize(), function(response){
console.log(response);
},'json' );
e.preventDefault();
});
这是有效的
jQuery("#contestform").validate();
但我不知道如何让他们一起工作。我希望只有在点击提交并且表单有效的情况下,顶部代码中的内容才会发生。我已经试过了,但是我想我不明白。我让它在一个点上工作,但你必须点击提交两次
一件事是,如果我能帮上忙的话,我希望将post()内容保存在一个单独的函数中,因为这两个部分将来自php的不同部分
编辑1
尝试过这个,但不起作用(在表单下)
(不工作,我的意思是它正常提交,而不是ajax)
edit2
我用jQuery.Form插件实现了它
尽管如此,我还是想知道我在上面的代码中做错了什么
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
}
});
submitHandler函数的参数不是jQuery事件,而是表单DOM对象。所以
<script>
$(function () {
$("form").validate({
submitHandler: function (form) {
$.post('/your_url/',
$(form).serialize(), function (response) {
console.log(response);
}, 'json');
}
});
});
</script>
$(函数(){
$(“表格”)。验证({
submitHandler:函数(表单){
$.post(“/your_url/”,
$(表单).serialize(),函数(响应){
控制台日志(响应);
}“json”);
}
});
});
或者将postForm函数的
<script>
$(function () {
function postForm(form) {
$.post('/your_url/',
$(form).serialize(),
function (response) { console.log(response); },
'json');
}
jQuery("form").validate({
submitHandler : postForm
});
});
</script>
$(函数(){
函数postForm(form){
$.post(“/your_url/”,
$(表单).serialize(),
函数(响应){console.log(响应);},
“json”);
}
jQuery(“表单”).validate({
提交者:postForm
});
});
请查看已编辑的问题,不工作,但我的功能设置可能不正确
<script>
$(function () {
$("form").validate({
submitHandler: function (form) {
$.post('/your_url/',
$(form).serialize(), function (response) {
console.log(response);
}, 'json');
}
});
});
</script>
<script>
$(function () {
function postForm(form) {
$.post('/your_url/',
$(form).serialize(),
function (response) { console.log(response); },
'json');
}
jQuery("form").validate({
submitHandler : postForm
});
});
</script>