简体   繁体   English

函数中的全局变量值不会更新

[英]Global variable value in function doesn't update

I'm using the the jQuery fileupload plugin and configure it like this: 我正在使用jQuery fileupload插件并将其配置如下:

jQuery(document).ready(function() {
  jQuery("#fileupload").fileupload({
    dataType: "json",
    url: "ajax_handler.php?globalVar=" + globalVar,
    send: function (e, data) {

    },
    done: function (e, data) {

    }
});

....

Where globalVar is (wait for it) a global variable. globalVar是(等待它)一个全局变量。

The problem is that if I change the value of globalVar and then do a file upload (using the jQuery file upload plugin which is AJAX so that the page doesn't change), the URL that the request is made to has the original globalVar value (that it had when the page first loaded). 问题是,如果我更改globalVar的值然后执行文件上传(使用jQuery文件上传插件,这是AJAX,以便页面不会更改),请求的URL具有原始globalVar值(当页面首次加载时它有)。

Why is this happening? 为什么会这样?

When you create the file upload widget, you're passing it a configuration object. 创建文件上载窗口小部件时,您将向其传递配置对象。 This object has a number of properties, among them url . 该对象具有许多属性,其中包括url

The property values are evaluated when the object is created (in your case when you create the file upload widget in $(document).ready() ). 在创建对象时评估属性值(在您$(document).ready()$(document).ready()创建文件上载小部件时)。 The object has no knowledge of the globalVar variable since the value that is assigned: 由于分配的值,对象不知道globalVar变量:

"ajax_handler.php?globalVar=" + globalVar

evaluates to a simple string (you're not passing it a reference to globalVar or anything like that). 求值为一个简单的字符串(你没有传递给globalVar或类似的东西)。 The behavior you seem to be expecting could only happen if you'd assign a function to the url property in which you reference globalVar (I don't know whether the file upload plugin supports this). 如果你将一个函数分配给你引用globalVarurl属性(我不知道文件上传插件是否支持这个),那么你似乎期望的行为才会发生。

So even if you change globalVar at a later time, the file upload widget's url configuration option will stay the same. 因此,即使您稍后更改globalVar ,文件上载小部件的url配置选项也将保持不变。 If you want to change it, you need to explicitly assign it again. 如果要更改它,则需要再次明确指定它。

If you want a variable value to persist from one page to another, then you have to store that value somewhere and then retrieve it from the other page. 如果您希望变量值从一个页面持久存储到另一个页面,则必须将该值存储在某个位置,然后从另一个页面检索它。 Javascript variables are local to a page so the entire javascript state is cleared each time you go to a new page. Javascript变量是页面的本地变量,因此每次进入新页面时都会清除整个javascript状态。 The options for storing/retrieving the variable are: 存储/检索变量的选项有:

  1. Store it and retrieve if from a cookie 存储它并从cookie中检索
  2. Store it and retrieve it from LocalStorage 存储它并从LocalStorage中检索它
  3. Store it and retrieve it on your server (probably using AJAX) 存储它并在服务器上检索它(可能使用AJAX)

The first two are simpler if you don't need it stored on the server. 如果您不需要将它存储在服务器上,前两个更简单。 The advantage of the server is that it can be available even from other computers. 服务器的优点是它甚至可以从其他计算机上获得。 The cookie and LocalStorage are only available on that particular computer. Cookie和LocalStorage仅在该特定计算机上可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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