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

时间:2022-08-24 13:51:07

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?

这个错误是什么意思?

5 个解决方案

#1


29  

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

似乎是您的一个值,带有“值”的属性键是未定义的。在执行if语句之前,对i1、i2和__i进行定义:

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'; }
}

#2


10  

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("用户名")[0]不返回任何元素。检查,所有元素都存在。

#3


7  

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
    }
}

#4


6  

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.

其他元素的密码也是类似的。

#5


5  

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

这里的帖子帮助我找到了解决未被捕获的类型错误的解决方案:无法读取未定义问题的属性值。

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.

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

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.

不同之处在于你做了一个检查(如果定义了一个属性),如果检查是正确的,那么你可以尝试给它赋值。

#1


29  

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

似乎是您的一个值,带有“值”的属性键是未定义的。在执行if语句之前,对i1、i2和__i进行定义:

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'; }
}

#2


10  

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("用户名")[0]不返回任何元素。检查,所有元素都存在。

#3


7  

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
    }
}

#4


6  

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.

其他元素的密码也是类似的。

#5


5  

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

这里的帖子帮助我找到了解决未被捕获的类型错误的解决方案:无法读取未定义问题的属性值。

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.

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

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.

不同之处在于你做了一个检查(如果定义了一个属性),如果检查是正确的,那么你可以尝试给它赋值。