简体   繁体   English

触发功能时出现“未捕获的TypeError:无法读取未定义的属性'名称'”

[英]“Uncaught TypeError: Cannot read property 'Name' of undefined” when triggered a function

EDIT: I found that the code is working fine and nothing is wrong. 编辑:我发现该代码工作正常,没有任何错误。 It's myself that was blurred. 是我自己被模糊了。 Sorry for not being cautious. 对不起,我很抱歉。

I have a HTML file with a form containing a textbox with ID #StuIDtxt. 我有一个HTML文件,其格式为包含ID为#StuIDtxt的文本框。

Also there is a few JavaScript, and here is a code in my file. 也有一些JavaScript,这是我文件中的代码。 The function getResult() would be triggered when the form is submitted. 提交表单时将触发功能getResult()。

function getResult(){
  var StuID = document.getElementById('StuIDtxt').value;
  document.getElementById('name').innerHTML = result[StuID].Name;
  return false;
}

There is also a variable name 'result' storing array parsed from JSON. 还有一个变量名“结果”存储从JSON解析的数组。

When I fill the textbox with '10001' and submit the form, JavaScript console said 当我在文本框中填写“ 10001”并提交表单时,JavaScript控制台说

Uncaught TypeError: Cannot read property 'Name' of undefined

However typing 但是打字

document.getElementById('name').innerHTML = result['10001'].Name;

in console result in successful execution as expected. 在控制台中导致按预期成功执行。

Did I do anything wrong in the function? 我在功能上做错了吗?

Live web here: https://srakrn.com/satitnpru/announce 此处的实时网络: https//srakrn.com/satitnpru/announce

The JSON retrieved from https://srakrn.com/satitnpru/announce/result_demo.json does not contain any id of 10001 . https://srakrn.com/satitnpru/announce/result_demo.json检索到的JSON不包含任何ID 10001 If I try 00001 the error does not occur. 如果我尝试00001不会发生此错误。 Change like this: 像这样更改:

function getResult(){
  var StuID = document.getElementById('StuIDtxt').value;
  if ( StuID != null && StuID.trim() != "" )
  {
       if ( typeof result[StuID] != "undefined" )
           document.getElementById('name').innerHTML = result[StuID].Name;
       else
           alert("Unknonw ID: " + StuID);
  }
  return false;
}

In your code, you assume that the given ID will always exist in result and directly reference Name . 在您的代码中,您假定给定的ID将始终存在于result并直接引用Name In case, StuID doesn't exist, this will cause a runtime error. 万一StuID不存在,将导致运行时错误。

Maybe it's because '10001' doesn't exist 也许是因为“ 10001”不存在

here is the log from the result var 这是来自result var的日志

http://prntscr.com/9mdsjv http://prntscr.com/9mdsjv

I used '00020' and its OK 我用了“ 00020”,还可以

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

相关问题 未捕获的TypeError无法读取未定义的属性“name” - Uncaught TypeError cannot read property 'name' of undefined 未捕获的TypeError:无法读取未定义的属性“名称” - Uncaught TypeError: Cannot read property "name' of undefined 未捕获的TypeError:无法读取未定义的属性“name” - Uncaught TypeError: Cannot read property 'name' of undefined 未捕获的TypeError:无法读取未定义的属性“名称” - Uncaught TypeError: Cannot read property 'Name' of undefined 未捕获的TypeError:无法读取未定义的属性“name” - Uncaught TypeError: Cannot read property 'name' of undefined 未捕获的类型错误:调用函数时无法读取未定义的属性“then” - Uncaught TypeError: Cannot read property 'then' of undefined when calling a function 未捕获的TypeError:无法读取函数中未定义的属性“ 0” - Uncaught TypeError: Cannot read property '0' of undefined at function 未捕获的类型错误:无法读取 tableToJson 中未定义的属性“名称” - Uncaught TypeError: Cannot read property 'name' of undefined at tableToJson 如何修复未捕获的类型错误:无法读取未定义的属性“名称” - How to fix Uncaught TypeError: Cannot read property 'name' of undefined 未捕获的TypeError:无法读取未定义的属性“ item_name” - Uncaught TypeError: Cannot read property 'item_name' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM