Javascript 无法读取未定义的属性“值”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14592134/
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-24 17:19:22  来源:igfitidea点击:

Cannot read property 'value' of undefined

javascript

提问by Sir

I have a simple form like this:

我有一个像这样的简单表格:

<form method="post" id="login">

Username: <input type="text" value="" name="usr" />
Password: <input type="password" value="" name="pw"/>
<input type="submit" id="log" name="log" value="Login" style="width:250px;"/><br/>

</form>

But i keep getting cannot read property for my javascript with this line:

但是我一直无法使用以下行读取我的 javascript 的属性:

var usr = login.usr.value;
var pw = login.pw.value;

What is the reason i get this error ?

我收到此错误的原因是什么?

回答by Mehdi Karamosly

Try to give your form a name and change your code like below :

尝试为您的表单命名并更改您的代码,如下所示:

<form method="post" id="login" name="login">

Username: <input type="text" value="" name="usr" />
Password: <input type="password" value="" name="pw"/>
<input type="submit" id="log" name="log" value="Login" style="width:250px;"/><br/>

</form>

and the in your javascript :

并且在您的 javascript 中:

var usr = document.login.usr.value;

You can check here (jsfiddle link).

您可以在此处查看(jsfiddle 链接)

回答by Diodeus - James MacFarlane

<input type="text" value="" name="usr" />does not magically become the variable login.usr, you need to define the value based on the DOM.

<input type="text" value="" name="usr" />不会神奇地变成变量login.usr,需要根据DOM定义值。

var usr = document.login.usr.value;

回答by KingKongFrog

Try using getElementsByName:

尝试使用getElementsByName

The getElementsByName()method accesses all elements with the specified name.

getElementsByName()方法访问具有指定名称的所有元素。

var usr = document.getElementsByName('usr')[0].value;
var pw = document.getElementsByName('pw')[0].value;

http://jsfiddle.net/L3RvL/

http://jsfiddle.net/L3RvL/

回答by mplungjan

Method 1:

方法一:

<form method="post" id="login">
  Username: <input type="text" value="" name="usr" />

var form = document.getElementById("login"); //DOM access
var usr = form.elements["usr"]; // Forms access

Method 2 - not valid xhtml:

方法 2 - 无效的 xhtml:

<form method="post" name="login">
  Username: <input type="text" value="" name="usr" />

var form = document.login; // Forms access
var usr = form.elements["usr"]; // Forms access

Method 3:

方法三:

<form method="post">
  Username: <input type="text" value="" name="usr" />

var usr = document.getElementsByName("usr")[0]; // first field on page named this

Method 4: - preferred these days since ID MUST be unique - less useful if there are more forms with usr on the page that needs same validation

方法 4: - 这些天首选,因为 ID 必须是唯一的 - 如果页面上有更多带有 usr 的表单需要相同的验证,则不太有用

<form method="post">
  Username: <input type="text" value="" id="usr" name="usr" />

var usr = document.getElementById("usr");

回答by KingKongFrog

Works in every browser:

适用于所有浏览器:

   document.forms["login"].elements["usr"].value;
   document.forms["login"].elements["pw"].value;