gpt4 book ai didi

javascript - "Uncaught TypeError: Cannot read property ' ecommerce' 未定义的可能原因"

转载 作者:行者123 更新时间:2023-11-28 01:55:22 26 4
gpt4 key购买 nike

我正在运行此代码,一旦文档准备好,就会调用脚本方法。另外,如果我放置调试器并警告其值,我可以获取该对象,但它会抛出错误“未捕获类型错误:无法读取未定义的属性‘电子商务’” 在本地运行时。

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
var jsonObj = {
"input": {
"ecommerce":
{
"id": 123,
"name": "isEcommerce",
"type": "boolean",
"actionType" : "radioButton",
"child": {"yes":["p","q","e","w"],
"no":["a","b","c","d"]}
},
"bad website":
{
"id": 2324,
"name": "isBadWebsite",
"type": "boolean",
"actionType" : "radioButton",
"child": {"yes":["erw","sd","sd","sd"],
"no":["sd","sd","sd","fd"]}
}
}
};
var data = jsonObj;
$(document).ready(function(data){
alert("HI!!");
alert(data);
alert(data.input);
alert(data.input.ecommerce);
});

function tackleEvent(obj){
alert("Clicked " + obj);

}
</script>
</head>
<title>Hello!</title>

<body>
</body>
</html>

错误跟踪:

   Uncaught TypeError: Cannot read property 'ecommerce' of undefined ui:34
(anonymous function) ui:34
f.Callbacks.n jquery.min.js:2
f.Callbacks.o.fireWith jquery.min.js:2
e.extend.ready jquery.min.js:2
c.addEventListener.B jquery.min.js:2

为什么它无法获取对象并在输入相同内容时工作?

最佳答案

如果您注意到前 2 个警报框,data 显示的值将是:

function(a,b) { return new e.fn.init(a,b,h) }

这是因为an alias of jQuery namespace已在以下函数内传递(带有名为 data 的本地副本):

function(data){
alert("HI!!");
alert(data);
alert(data.input);
alert(data.input.ecommerce);
}

并已命名为数据。因此,在上述函数内部;你可以使用:

data.ajax()
data.ready() //another
data.load()
// and all other jQuery functions.

访问全局变量data;您需要使用:window.data

关于javascript - "Uncaught TypeError: Cannot read property ' ecommerce' 未定义的可能原因",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19195757/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com