简体   繁体   English

调用输入字段变量时控制台日志返回空值

[英]Console log returning empty value when calling for input field variable

Why is console.log returning an empty line when calling for a text input's variable.为什么在调用文本输入的变量时 console.log 返回一个空行。

HTML HTML

                        <label for="subject">Subject</label>
                        <input type="text" id="subject" name="ticket-subject" />

Javascript Javascript

I tried the actual form and its console.log worked perfectly on submit, but somehow the value for ticketSubject seems to be empty.我尝试了实际的表单,它的console.log在提交时工作得很好,但不知何故ticketSubject的值似乎是空的。

const ticketForm = document.querySelector('.ticket-form')
const ticketSubject = document.getElementById('subject').value;

ticketForm.addEventListener('submit', (e)=> {
    e.preventDefault()
    console.log(ticketSubject)
    console.log('The form submit works')
})

Here's the subject field:这是主题字段:

But it returns nothing:但它什么都不返回:

You're reading the value of the input immediately when the page first loads, before the user has had a chance to enter a value.在页面首次加载时,您会在用户有机会输入值之前立即读取输入值。 Read the value in the submit event handler instead:改为读取submit事件处理程序中的值:

 const ticketForm = document.querySelector('.ticket-form') ticketForm.addEventListener('submit', (e)=> { e.preventDefault() // read the value here const ticketSubject = document.getElementById('subject').value; console.log(ticketSubject) console.log('The form submit works') })
 <form class="ticket-form"> <label for="subject">Subject</label> <input type="text" id="subject" name="ticket-subject" /> <input type="submit" value="Submit" /> </form>

You need to read the value inside the addEventListener() handler, otherwise the value is stored before the event and will never show up on the console.log()您需要读取addEventListener()处理程序中的值,否则该值会在事件发生之前存储,并且永远不会显示在console.log()

 const ticketForm = document.querySelector('.ticket-form') ticketForm.addEventListener('submit', e => { e.preventDefault() console.log(document.getElementById('subject').value); console.log('The form submit works') })
 <form class="ticket-form"> <label for="subject">Subject</label> <input type="text" id="subject" name="ticket-subject" /> <button type="submit"> submit</button> </form>

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

相关问题 参数在console.log上返回空值 - Argument is returning empty value on console.log 在变量上调用它时,console.log 打印“未定义” - console.log printing 'undefined' when calling this on a variable 尝试在 js 中声明全局变量时无法控制台输入字段值 - not able to console input field value when try to declare it global variable in js 为什么 checkValidity() 为类型为 email 且值为空的输入字段返回“true”? - why checkValidity() is returning "true" for input field of type email with empty value? 当我填写表单输入时,它会在控制台日志中显示上一个值 - when i fill a form input it shows the previous value in console log 如果输入字段值为空但包含变量,如何显示 div? - How to show a div if input field value is empty but contains a variable? 为什么在调用console.log的时候jest document.createElement('div')等元素都是空的? - Why jest document.createElement('div') and other elements are empty when calling console.log? 数组不使用控制台日志返回任何值 - Array not returning any value using console log 输入类型=密码值返回空值,除非我在Chrome控制台中使用断点 - Input type=password value returning empty unless i use a breakpoint in the chrome console Javascript:为什么在console.log中调用变量时将“()”放在变量之后? - Javascript: Why do I put “()” after a variable when calling it in console.log?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM