gpt4 book ai didi

jquery在回调函数后停止执行

转载 作者:行者123 更新时间:2023-12-01 02:36:33 26 4
gpt4 key购买 nike

我有一个简单的插件,用于提交表单

(function($){
$.fn.ajaxSubmit = function(options){
var defaults = {
onSubmit:function(){},
},
o = $.extend({},defaults, options);

return this.each(function(){
$(this).submit(function(e){
o.onSubmit.call(this);
$.post(o.url,$(this).serialize() ,
function(i){"do stuff"},'json');
return false;
});
});
}
})(jQuery);

我就是这么调用它的

$('#commentForm').ajaxSubmit({
onSubmit:function(){
if($('textarea').val()==''){ "do not execute $.post"}
}
});

我想检查textarea是否为空,如果是,则不要继续深入插件或不要执行$.post。

最佳答案

基于gap的答案:

(function($){
$.fn.ajaxSubmit = function(options){
var defaults = {
shouldSubmit:function(){ return true; },
},
o = $.extend({},defaults, options);

return this.each(function(){
$(this).submit(function(e){
if(o.shouldSubmit.call(this)) {
$.post(o.url,$(this).serialize(), function(i){"do stuff"},'json');
}
return false;
});
});
}
})(jQuery);

$('#commentForm').ajaxSubmit({
onSubmit:function(){
if($('textarea').val()==''){ return false; }
}
});

将函数名称更改为shouldSubmit,因为这样更有意义,并将默认值设置为返回true,因此如果没有被覆盖,它总是提交。

关于jquery在回调函数后停止执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5181783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com