简体   繁体   中英

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

I'm a noob and also new to this site, so let me know if there are things I should do to improve this post. Anyway, I have a function that is re-used frequently in my site, so I stored it in a global variable and want to call it when a certain button is clicked.

The code looks like this (see below). My problem is that although I can confirm that the button click tries to call the function, it is clearly never actually called (none of my alerts fire and the changes to the text fields are not saved). All of this is contained in the $(document).read(function...

Have I made a dumb mistake somewhere, or is there something I'm doing clearly wrong?

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

Well, I never figured out why this didn't work, but....

I just removed the global variable and created a separate function for saveAll() and it works. Interestingly, I have a second application using the same code that uses the Global.saveAll (with the same innards) and works fine, so I must have something unusual in one of my earlier lines.

Thanks for your suggestions!

Try setting window.Global = ... , since declaring var Global sets the scope to be within the ready closure.

Then you should be able to use it later.

我刚刚删除了全局变量并为 saveAll() 创建了一个单独的函数,它可以工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM