简体   繁体   English

为什么这个全局变量不会在数组中更新?

[英]Why won't this global variable update in array?

I'm making a small webapp in Javascript.我正在 Javascript 中制作一个小型 webapp。 I got every thing working except for 1 probably simple thing.除了一件可能很简单的事情外,我让所有事情都正常工作。 I have 2 global variables that have to stay global, because I have to use them later in other functions:我有 2 个全局变量必须保持全局,因为稍后我必须在其他函数中使用它们:

var staffmembername;

var data = {
  "personaldata": [{
      "name": staffmembername,
      "location": "Iowa"
    },
    {
      "name": "Cynthia",
      "location": "California",
    }
  ]

};

The 'data' array uses the 'stafmembername' variable. 'data' 数组使用 'stafmembername' 变量。 The 'stafmembername' variable change its value to a selected option value. 'stafmembername' 变量将其值更改为选定的选项值。 As you can see thanks to the test with alert.正如您所看到的,感谢带有警报的测试。 How ever the variable within the array seems to stay undefined.数组中的变量似乎仍未定义。

Rest of code: Rest 的代码:

document.getElementById("confirmchoice").onclick = function() {

  var e = document.getElementById("staffmember");
  pickedname = e.options[e.selectedIndex].text;
  document.getElementById("nr1").value = pickedname;
  staffmembername = document.getElementById("nr1").value;
  alert(staffmembername);

  document.getElementById("h3tl").innerHTML = "name:" + data.personaldata[0].name + "<br>"
  + "location: " + data.personaldata[0].location;

} 

If I put the variables within the function it al works, but then my other functions won't work.如果我将变量放在 function 中,它就可以工作,但是我的其他功能将无法工作。 So is there a way to update the variable within the array while keeping both variables global?那么有没有办法在保持两个变量全局的同时更新数组中的变量?

JSFIDDLE JSFIDDLE

This happens because your data object is already created and the name property inside it is not updated again.发生这种情况是因为您的数据 object 已经创建并且其中的 name 属性没有再次更新。 You can make the data Object by calling a method, this will take the latest values.您可以通过调用方法使数据 Object 获取最新值。

function getData() {
    return {
        "personaldata": [{
            "name": staffmembername,
            "location": "Iowa"
            },
            {
            "name": "Cynthia",
            "location": "California",
            }
        ]
    };
}

See this Fiddle看到这个小提琴


Another alternative could be to make the staffmembername as an Object and update its values, this way, you would be able to access updated object.另一种选择是将工作人员名称设为staffmembername并更新其值,这样您就可以访问更新的 object。

var staffmembername = {value: ''};

var data = {
  "personaldata": [{
      "name": staffmembername,
      "location": "Iowa"
    },
    {
      "name": "Cynthia",
      "location": "California",
    }
  ]

};

document.getElementById("confirmchoice").onclick = function() {
  var e = document.getElementById("staffmember");
  pickedname = e.options[e.selectedIndex].text;
  document.getElementById("nr1").value = pickedname;
  staffmembername.value = document.getElementById("nr1").value;
  alert(staffmembername);
  document.getElementById("h3tl").innerHTML = "name:" + data.personaldata[0].name.value + "<br>" + "location: " + data.personaldata[0].location;


}

See this Fiddle看到这个小提琴

staffmembername is evaluated when you initialize data , because it's passed by value.初始化data时会评估staffmembername ,因为它是按值传递的。 So its value in data never changes.所以它在data中的价值永远不会改变。 You could wrap data in a function.您可以将data包装在 function 中。 staffmembername would be evaluated every time you call the function, not only at initialization:每次调用staffmembername时都会评估 staffmembername,而不仅仅是在初始化时:

var staffmembername;

var data = function () {
   return {
    "personaldata": [{
      "name": staffmembername,
      "location": "Iowa"
    },
    {
      "name": "Cynthia",
      "location": "California",
    }
   ]
  };
 }
};

Then use it like this:然后像这样使用它:

data().personaldata[0].location

You could also use an object for staffmembername .您也可以使用 object 作为staffmembername

var staffmembernameObject = {};

var data = [{
  personaldata: [{
    name: staffmembernameObject,
...
staffmembernameObject.data = document.getElementById("nr1").value;

And use it like this: data.personaldata[0].name.data并像这样使用它:data.personaldata[0].name.data

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

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