[英]accessing global variables after setting them javascript
我试图跟踪对Django中选择框的更改。 我下面的代码正在努力alert(change.new_time);
,因此我正确地制作了对象-
var changed_select_box_array = [];
function handleChanges(id){
var x = document.getElementById(id).selectedIndex;
var time = document.getElementsByTagName("option")[x].value;
var change = {id:id, new_time:time};
alert(change.id);
alert(change.new_time);
changed_select_box_array[changed_select_box_array.length] = change;
alert(changed_select_box_array[0].id);
}
但我无法访问数组中的新项目。 我尝试了4-5种不同的方法,并遵循了在此站点上找到的func中的全局变量的某些规则,但无法从新数组访问任何内容。 我在添加阵列时做错了吗? 我也尝试过推。 谢谢
您可以将Object用作关联数组。
var changed_select_box_array = {}; function handleChanges() { var x = this.selectedIndex; var id = this.id; var time = this.getElementsByTagName("option")[x].value; var change = { id: id, new_time: time }; changed_select_box_array[id] = change; console.log(changed_select_box_array); }
<!--Emitation of some select inputs with change events--> <select id="s1" onchange="handleChanges.call(this)"> <option value="val1">Value 1</option> <option value="val2">Value 2</option> <option value="val3">Value 3</option> </select> <select id="s2" onchange="handleChanges.call(this)"> <option value="val4">Value 1</option> <option value="val5">Value 2</option> <option value="val6">Value 3</option> </select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.