【发布时间】:2016-03-30 18:21:23
【问题描述】:
我是一个菜鸟,也是这个网站的新手,所以如果有什么我应该做的事情来改进这篇文章,请告诉我。无论如何,我有一个在我的站点中经常重复使用的函数,所以我将它存储在一个全局变量中,并希望在单击某个按钮时调用它。
代码如下所示(见下文)。我的问题是,虽然我可以确认按钮单击尝试调用该函数,但显然从未真正调用过它(我的警报都没有触发,对文本字段的更改也没有保存)。所有这些都包含在$(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(具有相同的内部结构)并且工作正常,所以我在前面的一行中一定有一些不寻常的东西。
感谢您的建议!
【问题讨论】:
-
点击时有没有报错?
-
您的
i缺少var,但这不是问题。 -
您所说的“所有这些都包含在 $(document).read(function”中到底是什么意思,您也可以显示该代码吗?这可能很容易成为问题,因为
var Global不再是全球性的。 -
点击时没有出现任何错误,这是奇怪的部分。至于我的代码中的位置,顶部看起来像
-
刚刚注意到,您的 IIFE 缺少调用
()(并且无论如何都非常无用,因为其中没有局部变量)。不过,应该仍然抛出异常。
标签: javascript jquery global-variables