【问题标题】:Why is input field.type returning undefined?为什么输入 field.type 返回未定义?
【发布时间】:2021-04-17 20:19:07
【问题描述】:

我有一个输入元素:

<input type="password" name="password1" autocomplete="new-password" placeholder="Password" disabled class="register_field" required id="id_password1">

当我将它分配给一个变量并记录它的类型时:

const password1 = $("#id_password1");
console.log(password1.type);

它返回“未定义”。为什么不用密码?

我认为这可能是输入的初始 disabled 属性,或者表单本身的 novalidate 属性,或者因为我使用 Django 的模板引擎来生成表单,但没有一个原因。非常感谢您的任何建议。

【问题讨论】:

标签: javascript html


【解决方案1】:

如果您使用 jQuery,$(...) 返回一个 jQuery 对象,而不是 DOM 元素。在这种情况下,您想致电.attr('type')

console.log($('#field').attr('type'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="password" id="field">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-24
    • 2021-12-11
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    相关资源
    最近更新 更多