Javascript jQuery插件:回调选项必须返回false(不工作)
所以我建立了一个插件,它有一个回调选项。这个回调用作验证部分,所以我们使用“return false”来停止插件,但我无法让它工作 因此,回调正在工作,但返回false不起作用(它必须是返回false,而不是某种布尔变量) //回调Javascript jQuery插件:回调选项必须返回false(不工作),javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,所以我建立了一个插件,它有一个回调选项。这个回调用作验证部分,所以我们使用“return false”来停止插件,但我无法让它工作 因此,回调正在工作,但返回false不起作用(它必须是返回false,而不是某种布尔变量) //回调 $('.a').click(function(){ if(typeof options.onValidate == 'function'){ options.onValidate.call(this); } // if the ca
$('.a').click(function(){
if(typeof options.onValidate == 'function'){
options.onValidate.call(this);
}
// if the callback has a return false then it should stop here
// the rest of the code
});
//选择权
....options = {
// more options
onValidate:function(){
//some validation code
return false;//not working
}
}
返回false,但它无法停止单击处理程序的执行。你应使用:
if(typeof options.onValidate == 'function'){
var result = options.onValidate.call(this);
if(result === false) return;
}
您没有在代码中使用返回的布尔值。试试这个:
$('.a').click(function() {
var isValid = false;
if (typeof options.onValidate == 'function'){
isValid = options.onValidate.call(this);
}
if (isValid) {
// if the callback has a return false then it should stop here
// the rest of the code
}
});
$('.a').click(function() {
var isValid = false;
if (typeof options.onValidate == 'function'){
isValid = options.onValidate.call(this);
}
if (isValid) {
// if the callback has a return false then it should stop here
// the rest of the code
}
});