Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery插件:回调选项必须返回false(不工作)_Javascript_Jquery_Jquery Plugins - Fatal编程技术网

Javascript jQuery插件:回调选项必须返回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

所以我建立了一个插件,它有一个回调选项。这个回调用作验证部分,所以我们使用“return false”来停止插件,但我无法让它工作

因此,回调正在工作,但返回false不起作用(它必须是返回false,而不是某种布尔变量)

//回调

$('.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
    }
});