在下面的代码中,我将一个全局变量设置为json字符串,但是当我尝试在el.on函数中访问它时,它的日志为null。如果我改变了child_strings =' test&#39 ;;它工作正常。
发生了什么事?
<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>
答案 0 :(得分:1)
您的申请中必须有另一个范围。您提供的代码可以正常使用:http://jsfiddle.net/afzza446/
检查变量的声明是否在范围内。你可以尝试像这样声明变量(没有var
):
child_strings = JSON.parse('{"...
或者将其显式添加到全局窗口对象:
window.child_strings = ...