在下面的代码中,我将一个全局变量设置为json字符串,但当我试图在el.on函数中访问它时,它的日志为null。如果我将child_strings更改为'test';它运行良好。
发生了什么事?
<script type="text/javascript">
var child_strings = JSON.parse('{"Open":["Open_New","Closed_Closed","Open_Pending Input","Open_Pending","Open_CARD","Open_Open","Closed_Rejected"],"Closed":["Open_DAD","Closed_Duplicate","Open_Assigned"]}');
if ("1") {
if (typeof de_entries == 'undefined') {
var de_entries = new Array;
}
var el = $("#state");
el.on('change', function() {
console.log(child_strings);
alert(child_strings);
updateDynamicEnum("state", "status", child_strings)
});
}
</script>
应用程序中必须有另一个作用域。您提供的代码运行良好:http://jsfiddle.net/afzza446/
检查变量的声明是否不在作用域中。您可以尝试这样声明变量(没有var
):
child_strings = JSON.parse('{"...
或者显式地将其添加到全局窗口对象:
window.child_strings = ...