Javascript 未捕获的类型错误:无法读取未定义的属性“值”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6550795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:15:29  来源:igfitidea点击:

Uncaught TypeError: Cannot read property 'value' of undefined

javascripttypeerror

提问by John

I have some JavaScript code that gives this error

我有一些 JavaScript 代码会出现此错误

Uncaught TypeError: Cannot read property 'value' of undefined

Code

代码

var i1 = document.getElementById('i1');
var i2 = document.getElementById('i2');
var __i = {'user' : document.getElementsByName("username")[0], 'pass' : document.getElementsByName("password")[0] };
if(  __i.user.value.length >= 1 ) { i1.value = ''; } else { i1.value = 'Acc'; }

if(  __i.pass.value.length >= 1 ) { i2.value = ''; } else { i2.value = 'Pwd'; }

What does this error mean?

这个错误是什么意思?

采纳答案by OsQu

Seems like one of your values, with a property key of 'value' is undefined. Test that i1, i2and __iare defined before executing the if statements:

似乎是您的值之一,属性键为 'value' 未定义。测试i1,i2__i在执行 if 语句之前定义:

var i1 = document.getElementById('i1');
var i2 = document.getElementById('i2');
var __i = {'user' : document.getElementsByName("username")[0], 'pass' : document.getElementsByName("password")[0] };
if(i1 && i2 && __i.user && __i.pass)
{
    if(  __i.user.value.length >= 1 ) { i1.value = ''; } else { i1.value = 'Acc'; }

    if(  __i.pass.value.length >= 1 ) { i2.value = ''; } else { i2.value = 'Pwd'; }
}

回答by nfechner

Either document.getElementById('i1'), document.getElementById('i2'), or document.getElementsByName("username")[0]is returning no element. Check, that all elements exist.

要么document.getElementById('i1')document.getElementById('i2')或者document.getElementsByName("username")[0]是没有返回元素。检查,所有元素都存在。

回答by Joe L.

Try this, It always works, and you will get NO TypeError:

试试这个,它总是有效,你不会得到任何类型错误:

try{

    var i1 = document.getElementById('i1');
    var i2 = document.getElementById('i2');
    var __i = {'user' : document.getElementsByName("username")[0], 'pass' : document.getElementsByName("password")[0] };
    if(  __i.user.value.length >= 1 ) { i1.value = ''; } else { i1.value = 'Acc'; }
    if(  __i.pass.value.length >= 1 ) { i2.value = ''; } else { i2.value = 'Pwd'; }

}catch(e){
    if(e){
    // If fails, Do something else
    }
}

回答by Abhijit

First, you should make sure that document.getElementsByName("username")[0] actually returns an object and not "undefined". You can simply check like

首先,您应该确保 document.getElementsByName("username")[0] 实际上返回一个对象而不是“未定义”。你可以简单地检查一下

if (typeof document.getElementsByName("username")[0] != 'undefined')

Similarly for the other element password.

其他元素密码也类似。

回答by Festus Tamakloe

The posts here help me a lot on my way to find a solution for the Uncaught TypeError: Cannot read property 'value' of undefinedissue.

此处的帖子在我为Uncaught TypeError: Cannot read property 'value' of undefinedissue寻找解决方案的过程中帮了我很多忙。

There are already here many answers which are correct, but what we don't have here is the combination for 2 answers that i think resolve this issue completely.

这里已经有很多正确的答案,但我们这里没有的是我认为完全解决这个问题的 2 个答案的组合。

function myFunction(field, data){
  if (typeof document.getElementsByName("+field+")[0] != 'undefined'){
  document.getElementsByName("+field+")[0].value=data;
 }
}

The difference is that you make a check(if a property is defined or not) and if the check is true then you can try to assign it a value.

不同之处在于您进行检查(是否定义了属性),如果检查为真,那么您可以尝试为其分配一个值。

回答by Mahmoud Mostafa

You can just create a function to check if the variable exists, else will return a default value :

您可以创建一个函数来检查变量是否存在,否则将返回一个默认值:

function isSet(element, defaultVal){

    if(typeof element != 'undefined'){

        return element;

    }

    console.log('one missing element');

    return defaultVal;
}

And use it in a variable check:

并在变量检查中使用它:

var variable = isSet(variable, 'Default value');

回答by Kamil Kie?czewski

You code looks like automatically generated from other code - you should check that html elements with id=i1and i2and name=usernameand passwordexists before processing them.

您的代码看起来像是从其他代码自动生成的 -在处理它们之前,您应该检查带有id=i1andi2name=usernameand 的html 元素是否password存在。