简体   繁体   English

输入未在控制台中显示其值

[英]Input doesn't show its value in console

I have written this:我写过这个:

var $emailLogin = document.querySelector('#emailLogin')
var Email = ''

function emailLogin () {
    Email = this.value
}

$emailLogin.addEventListener('input', emailLogin)

function myFunction() {
    console.log(Email)
}

$loginBtn.addEventListener('click', MyFunction)

But console doesn't show input's value但控制台不显示输入的值单击登录按钮后,它不会改变

It's empty like shown.它是空的,如图所示。 I have no idea how to fix it我不知道如何解决它

You should try and avoid global variables.您应该尽量避免使用全局变量。 The following will get you what you want without any "collateral damage" to the global name space:以下内容将为您提供所需的内容,而不会对全局名称空间造成任何“附带损害”:

 document.querySelector('#button').addEventListener('click',function(ev){ console.log( document.querySelector('#emailLogin').value ) })
 <input type="text" id="emailLogin"><button id="button">login</button>

In JavaScript, you could try this:在 JavaScript 中,你可以试试这个:

document.getElementById('ButtonID').addEventListener('click',function(){
console.log(document.getElementById('emailLogin').value);
});

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

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