簡體   English   中英

jQuery插件函數返回未定義的值

[英]jQuery Plugin Function Returning Undefined For The Value

我創建了一個jQuery插件來處理Cookie。 我遇到的麻煩是我創建的用於讀取cookie並返回值的函數。 不管我做什么,當我從主頁調用函數時

test = $.cookie({
        action:'get', 
        cookie_name:LOGIN_COOKIE_NAME
    });

返回值始終是不確定的。 控制台輸出如下:

Value found is => 10
Returned => undefined

我盡力想弄清楚這里發生了什么。 我已經嘗試了所有我想做的事情,並且花了數小時來搜索,沒有解決方案。 以下是我的jQuery插件中的代碼:

//獲取所需cookie的值

  function get_cookie(){ var value = ''; // Get the name to search for var name = settings.cookie_name; // Divide the cookie string into an array var ca = document.cookie.split(';'); // Loop through each element in the main array we just created match_found = false; for(var i=0; i<ca.length; i++){ // Clear the whitespace from the ends to prevent mismatches and split each > element into the cookie name and value var temp = ca[i].trim().split('='); // Now check to see if this is the cookie we are looking for if(temp[0] == name && !match_found){ match_found = true; // If it is, then return the cookie's value cookie_value = temp[1]+''; if(settings.action != 'check'){ value = cookie_value; }else{ value = true; } } } if(!match_found && settings.action == 'check'){ return false; }else if(!match_found && settings.action != 'check'){ return 'dooby'; }else{ console.log('Value found is => '+value); return value; } } 

是否有人對導致此問題的原因以及如何糾正它有任何見解?

這是我的jQuery插件的完整代碼

(function($){
$.cookie = function(options) {

    // Default Settings
    var settings = $.extend({
        action : 'check', // create or delete
        cookie_name : 'cookie',
        cookie_value : 'default',
        expiration : 'Sun, 31 Dec 2017 12:00:00 GMT',
        path : '/'
    }, options);

    switch(settings.action){
        case "create":
            create_cookie();
            break;
        case "delete":
            delete_cookie();
            break;
        case "check":
            return check_for_cookie();
            break;
        case "get":
            get_cookie();
            break;
        default:
            get_cookie();
            break;
    }






    // Create the cookie
    function create_cookie(){
        try{
            document.cookie = settings.cookie_name+"="+settings.cookie_value+"; expires="+settings.expiration+"; path="+settings.path;
            console.log("Cookie Created");
        }catch(e){
            console.log(e);
        }
    }


    // Delete said cookie
    function delete_cookie(){
        document.cookie = settings.cookie_name+"="+settings.cookie_value+"; expires=Sun, 29 Dec 2013 12:00:00 GMT; path="+settings.path;
    }


    // Get the value of the sought after cookie
    function get_cookie(){
        var value = '';
        // Get the name to search for
        var name = settings.cookie_name;

        // Divide the cookie string into an array
        var ca = document.cookie.split(';');

        // Loop through each element in the main array we just created
        match_found = false;
        for(var i=0; i<ca.length; i++){
            // Clear the whitespace from the ends to prevent mismatches and split each element into the cookie name and value
            var temp = ca[i].trim().split('=');

            // Now check to see if this is the cookie we are looking for
            if(temp[0] == name && !match_found){
                match_found = true;
                // If it is, then return the cookie's value
                cookie_value = temp[1]+'';
                if(settings.action != 'check'){
                    value = cookie_value;
                }else{
                    value = true;
                }
            }
        }
        if(!match_found && settings.action == 'check'){
            return false;
        }else if(!match_found && settings.action != 'check'){
            return 'dooby';
        }else{
            console.log('Value found is => '+value);
            return value;
        }
    }


    // Check to see if said cookie exists
    function check_for_cookie(){
        var is_set = get_cookie(settings.cookie_name);
        if(is_set){
            return true;
        }else{
            return false;
        }
    }
};
}(jQuery));

解:

感謝下面的OJay敏銳的眼光和解決方案。 向下滾動並給他一些投票。

在主switch語句中對get_cookie()的調用get_cookie()該調用處理了要執行的操作)沒有返回任何內容。 因此,遵循OJay的解決方案,它現在可以像下面這樣工作:

case "get":
    return get_cookie();
    break;

在Cookie“插件”中,您對操作執行了case語句,並調用了相應的private方法。 現在,get方法返回一個值,但是您並未對其執行任何操作,get函數的返回值未由父函數返回

 switch(settings.action){
        ///... snipped for brevity
        case "get":
            //the get_cookie returns something, but you are not returning it past here
            get_cookie(); 
            break;
        ///... snipped for brevity
    }

這意味着父函數($ .cookie)中沒有用於獲取的return語句,因此返回未定義(默認javascript行為)

返回get操作的值,就像您對"check"操作所做的一樣。

它也沒有"create""delete"的返回值,但是它們不一定需要。 盡管我可能會返回一個布爾值(true或false)以指示創建或刪除成功

檢查符號$是否已定義,以及符號jQuery是否已定義。 如果未定義$jQuery是,請嘗試使用jQuery.cookie而不是$.cookie

如果兩者均未定義,我將檢查HTML代碼中是否包含jQuery腳本。 這里介紹包括jQuery一種方法。 在頁面的HTML代碼中查找類似<script src="jquery.. ,以查看是否已包含該腳本。如果尚未包含,則應包含它。在嘗試使用$jQuery符號之前,應包含這些腳本。

嘗試使用定義您的插件

$.fn.cookie = function(options) {
    //... your plugin
}

根據jQuery 文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM