$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - Can i use a function name in my jquery plugin parameters? - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Can i use a function name in my jquery plugin parameters? - Stack Overflow

matteradmin16PV0评论

I am creating a form validation plugin for jQuery and would like it to call a function once the form has been successfully validated. The plugin will have a default callback function, but I would like to modify this through the options parameter. Unfortunately what i have (below) does not work. Any ideas?

(function($){
    $.fn.extend({
        validify : function(options) {
            var defaults = {
                callback: "callbackFunc",
            };

            var options = $.extend(defaults,options);

            return this.each(function(){
                //validation code here
                //if valid call the function
                if(errors==0){
                    options.callback;
                }


            function callBackFunc(){
                // the default callback function
            }

            ...

I am creating a form validation plugin for jQuery and would like it to call a function once the form has been successfully validated. The plugin will have a default callback function, but I would like to modify this through the options parameter. Unfortunately what i have (below) does not work. Any ideas?

(function($){
    $.fn.extend({
        validify : function(options) {
            var defaults = {
                callback: "callbackFunc",
            };

            var options = $.extend(defaults,options);

            return this.each(function(){
                //validation code here
                //if valid call the function
                if(errors==0){
                    options.callback;
                }


            function callBackFunc(){
                // the default callback function
            }

            ...
Share Improve this question edited Jun 12, 2009 at 5:58 Paolo Bergantino 489k82 gold badges522 silver badges437 bronze badges asked Jun 12, 2009 at 5:55 Neil MillsNeil Mills
Add a ment  | 

2 Answers 2

Reset to default 6

Remove the quotes and you're golden.

This will pass a function reference. You can then call it by doing options.callback();

You will also need to declare the function before you pass the reference along. You could get around this by doing this instead:

callback: function() { callbackFunc(); }

Pass the function itself, rather than its name (ie, remove the quotes):

(function($){
    function callBackFunc(){
      // the default callback function
    }

    $.fn.extend({
        validify : function(options) {
            var defaults = {
                callback: callbackFunc // IMPORTANT: remove quotes AND trailing ma
            };

            var options = $.extend(defaults,options);

            return this.each(function(){
                //validation code here
                //if valid call the function
                if(errors==0){
                    options.callback();  // note parentheses
                }

            ...
Post a comment

comment list (0)

  1. No comments so far