簡體   English   中英

無法將文本框值存儲在變量中

[英]Can't store text-box value in variable

我正在嘗試創建一個簡單的聊天室程序,該程序從 html 文本框接收數據並將其顯示在容器內。 我嘗試通過將文本框值設置為新創建的段落元素來實現這一點,但由於某種原因,我無法將文本框值存儲到另一個變量中。

當我 console.log 文本變量時,它應該顯示我輸入的文本,但它只顯示一個空字符串。

const container = document.querySelector('.container');
let textBox = document.querySelector('.text-box');
let para = document.createElement('p');
let text = textBox.value;

 /*
textBox.addEventListener('keyup', (event) => {
    if (event.keycode === 13) {

    }
})
*/

para.textContent = text;
container.appendChild(para);
//console.log(textBox.value) => displays the value that I enter into the field.
//console.log(text) => displays ""

您可以嘗試替換 para.textContent = text Box.value

我希望它能解決問題。

謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM