简体   繁体   English

为什么 jQuery.val() 什么也不返回?

[英]Why is jQuery .val() returning nothing?

I am trying to get the value of an input.我正在尝试获取输入的值。 Here is what my form looks like:这是我的表格的样子:

在此处输入图像描述

This is the HTML for it:这是它的 HTML:

<label for="userName"></label>
<input type="text" class="form-input plain-text" id="userName" placeholder="Username">

Here is the jQuery to get its value and log it:这是 jQuery 获取其值并记录它:

$("#loginSubmit").click(function() {
    var userName = $("#userName").val();
    console.log(userName);
});

But when I type in some text and hit the login button that has the id #userName , the console returns this:但是当我输入一些文本并点击 ID #userName的登录按钮时,控制台会返回:

在此处输入图像描述

Why does this not output dwedwed in the console?为什么这不是 output dwedwed在控制台?

Thanks谢谢

Immediate reasons I can see are:我能看到的直接原因是:

  • you are attaching an event before the DOM is even loaded (if you're loading JS in the head instead of bottom of body for example)您甚至在加载 DOM 之前附加了一个事件(例如,如果您在头部而不是底部加载 JS)
  • multiple element's with the same ID具有相同 ID 的多个元素
  • you have a stopPropagation on button elements which is blocking the callback您在阻止回调的按钮元素上有一个 stopPropagation

Other than the above, its hard to determine what is going on without more code/information.除了上述之外,如果没有更多代码/信息,很难确定发生了什么。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM