简体   繁体   English

JS:“ TypeError:'未定义'不是对象”

[英]JS: “TypeError: 'undefined' is not an object”

var name = $("input[#name]").value;
var email = $("input[#email]").value;
var msg = $("input[#msg]").value;

var nameLength = name.length;
var emailLength = email.length;
var msgLength = msg.length;

As you can see, all I'm trying to do is to get the length of the value of the #email field. 如您所见,我要做的就是获取#email字段的值的长度。 It works for both of the other fields, but not the email field. 它适用于其他两个字段,但不适用于电子邮件字段。 It give "TypeError: 'undefined' is not an object". 它给出“ TypeError:'未定义'不是对象”。

Any reason why? 有什么原因吗?

I've tried in both Safari and Google Chrome, both gives the same error. 我在Safari和Google Chrome中都尝试过,但都给出了相同的错误。

The HTML: HTML:

<div class="form-group">
  <label class="col-md-4 control-label" for="email">Email</label>  
  <div class="col-md-5">
    <input id="email" name="email" type="text" placeholder="Enter your email address
    here" class="form-control input-md" required="">
  </div>
</div>

You are trying to use a DOM property on a jQuery object. 您正在尝试在jQuery对象上使用DOM属性。 If you have a jQuery object, then you must either use the jQuery method .val() , not the DOM property .value or you have to reach into the jQuery object to get the DOM object which you can then use DOM properties on. 如果您有jQuery对象,则必须使用jQuery方法.val() ,而不是DOM属性.value或者必须进入jQuery对象以获得DOM对象,然后可以在其上使用DOM属性。

I'd suggest you change it to use the jQuery method for getting the value: 我建议您将其更改为使用jQuery方法获取值:

var name = $("input[#name]").val();
var email = $("input[#email]").val();
var msg = $("input[#msg]").val();

Or, for illustrative purposes, this would also work (get the DOM object out of the jQuery object and use .value ): 或者,出于说明目的,这也将起作用(从jQuery对象中获取DOM对象并使用.value ):

var name = $("input[#name]")[0].value;
var email = $("input[#email]")[0].value;
var msg = $("input[#msg]")[0].value;

Based on the HTML you've added to your question, you need to fix your selectors too. 根据您添加到问题中的HTML,您还需要修复选择器。 To target an id="xxx" , you just use $("#xxx") . 要定位id="xxx" ,只需使用$("#xxx") No other part of the selector is required because ids must be unique in the entire document. 选择器的其他部分不需要,因为id在整个文档中必须是唯一的。 So, if all your HTML works like the email part you've shown in your question, then you can use this: 因此,如果您所有的HTML都像问题中显示的电子邮件部分一样工作,则可以使用以下代码:

var name = $("#name").val();
var email = $("#email").val();
var msg = $("#msg").val();

暂无
暂无

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

相关问题 JS setter 返回 ''undefined'' 并且 getter 导致 Uncaught TypeError TypeError: Cannot convert undefined or null to object - JS setters return ''undefined'' and the getter causes Uncaught TypeError TypeError: Cannot convert undefined or null to object React JS:TypeError:无法将未定义或空值转换为对象 - React JS: TypeError: Cannot convert undefined or null to object TypeError:“未定义”不是对象(正在评估“ $ .fn”)-Wordpress&shuffle.js - TypeError: 'undefined' is not an object (evaluating '$.fn') - Wordpress & shuffle.js 未捕获的类型错误:无法将未定义或 null 转换为 object React JS - Uncaught TypeError: Cannot convert undefined or null to object React JS 渲染错误:“TypeError:无法在 vue js 中将未定义或 null 转换为对象”? - Error in render: “TypeError: Cannot convert undefined or null to object” in vue js? js -Uncaught TypeError: Cannot read property 'x' of undefined at object (ball - js -Uncaught TypeError: Cannot read property 'x' of undefined at object (ball 映射函数 - 未处理的 JS 异常:TypeError:undefined 不是评估映射的对象 - Map Function - Unhandled JS Exception: TypeError: undefined is not an object evaluating map Next.js useContext TypeError:无法解构“对象(…)(…)”,因为它未定义 - Next.js useContext TypeError: Cannot destructure 'Object(…)(…)' as it is undefined TypeError:q未定义(在JS中) - TypeError: q is undefined (in JS) TypeError:“未定义”对象是否不可迭代? - TypeError: 'Undefined' object is not iterable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM