簡體   English   中英

存儲在全局變量中的函數在調用時不運行

[英]Function stored in a global variable doesn't run when called

我是一個菜鳥,也是這個網站的新手,所以如果我應該做些什么來改進這篇文章,請告訴我。 無論如何,我有一個在我的站點中經常重復使用的函數,所以我將它存儲在一個全局變量中,並希望在單擊某個按鈕時調用它。

代碼如下所示(見下文)。 我的問題是,雖然我可以確認按鈕單擊嘗試調用該函數,但它顯然從未真正被調用過(我的警報均未觸發,對文本字段的更改未保存)。 所有這些都包含在$(document).read(function...

我是不是在某個地方犯了一個愚蠢的錯誤,或者我做錯了什么?

$(document).ready(function () {

//Description:
//Global wrapper variable that contains all global functions. These include:
//  1. saveAll: Saves all values not stored in session data to hidden fields     - this includes
//          all added ingredient information. This allows us to manually pass values between 
//          client and server to save to db and also means we can eliminate Null values in table 
//          storage using a manual delimiter.
//----------------------------------------------------------------------------------------------
var Global = (function () {
    return {
        saveAll: function () {
            alert("entering save");
            //start by creating an array and initializing the length of the for loop 
            var saveValues = [];
            var numVals = $('#HidRowCt').val();

            alert("numVals: " + numVals);
            //Now loop through each ingredient row and create a string containing all textbox values
            //in this case, we'll do so by creating an array and then combining the values with a custom delimiter
            //the strings will then be saved, one by one, into the saveValues array, which will be serialized as a JSON object,
            //stored in a hidden field, and passed to the server
            for (i = 1; i < numVals; i++) {
                var TxtIngName = $('#TxtIngName' + i).val();
                var TxtIngNumUnits = $('#TxtIngNumUnits' + i).val();
                var SelIngUnits = $('#SelIngUnits' + i).val();
                //make temporary array and string
                var saveArr = new Array(TxtIngName, TxtIngNumUnits, SelIngUnits);
                var saveStr = saveArr.join("-||-");

                saveValues.push(saveStr);
            }

            alert("Save Values: " + saveValues);
            //this will automatically escape quotes, delimited with ","
            var jsoncvt = JSON.stringify(saveValues);

            $("#HidSave").val(jsoncvt);
        }
    };
});

//----------------------------------------------------------------------------------------------
//Description:
//Hijack the click event for the save button. Saves values not saved in session data.
//
//Functions:
//  Global.saveAll()
//----------------------------------------------------------------------------------------------
$("#SaveChanges").data.clickEvent = $("#SaveChanges").attr('onClick'); //save onclick event locally
$("#SaveChanges").removeAttr('onClick'); //and remove the onclick event
$('#SaveChanges').on('click', function (event) {

   Global.saveAll();
    //eval($("#SaveChanges").data.clickEvent); //now go ahead with the click event
});

好吧,我從來沒有想過為什么這不起作用,但是......

我剛剛刪除了全局變量並為 saveAll() 創建了一個單獨的函數,它可以工作。 有趣的是,我有第二個應用程序使用相同的代碼,使用 Global.saveAll(具有相同的內部結構)並且工作正常,所以我必須在我之前的一行代碼中有一些不尋常的東西。

感謝您的建議!

嘗試設置window.Global = ... ,因為聲明var Global將范圍設置在就緒閉包內。

然后你應該可以稍后使用它。

我剛剛刪除了全局變量並為 saveAll() 創建了一個單獨的函數,它可以工作。

暫無
暫無

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

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