问题描述
表单之前验证与这插件。并提交如图所示code:
The form is validated before submit with this plugin. And submitted as shown in the code:
jQuery.validator.setDefaults({
submitHandler: function(){
jQuery("form").submit(function(e) {
e.preventDefault();
jQuery.post(base_url + "blog/ajax_comment", { author: jQuery("#author").val(), email: jQuery("#email").val(), url: jQuery("#url").val(), message: jQuery("#message").val() , post_ID: jQuery("#post_ID").val(), not: jQuery("#not").val() }, function (data) {
//do stuff here
});
});
}
});
jQuery().ready(function() {
// validate the comment form when it is submitted
jQuery("#commentform").validate();
});
为了要提交,我需要preSS提交按钮两次。我是一种新的jQuery,我真的不能告诉这是为什么。有没有一种简单的方法来解决这个问题?
In order to be submitted, I need to press the submit button twice. I am kind of new to jQuery, and I can't really tell why is that. Is there an easy way to fix this?
推荐答案
在 submitHandler
已经覆盖默认的事件,所以只需将code为AJAX提交的有:
The submitHandler
is already overriding the default event, so just place the code for the ajax submission in there:
jQuery.validator.setDefaults({
submitHandler: function(){
jQuery.post(base_url + "blog/ajax_comment", { author: jQuery("#author").val(), email: jQuery("#email").val(), url: jQuery("#url").val(), message: jQuery("#message").val() , post_ID: jQuery("#post_ID").val(), not: jQuery("#not").val() }, function (data) {
//do stuff here
});
}
});
如果你的元素有名称
属性匹配其ID尽管这,只是使用的,像这样的:
If you elements have name
attributes that match their IDs though, just use .serialize()
, like this:
jQuery.validator.setDefaults({
submitHandler: function(form){
jQuery.post(base_url + "blog/ajax_comment", jQuery(form).serialize(), function (data) {
//do stuff here
});
}
});
这是怎么回事你的电流的code是,在提交
处理程序,它的附着 新的提交
处理程序的下一步的时间,而不是实际运行您的code。相反,只是在 submitHandler
回调直接运行code。
What's happening with your current code is that in the submit
handler, it's attaching a new submit
handler for next time, rather than actually running your code. Instead, just run your code directly in the submitHandler
callback.
这篇关于需要preSS与AJAX提交表单提交两次(jQuery的验证插件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!