所以我試圖用javascript獲取輸入標簽的值并將其顯示在控制臺上,但它根本不起作用。這是我的代碼,我在這里缺少什么?
const userTextValue = document.getElementById('user-text').value;
const show = () => {
console.log(userTextValue);
}
const submit = document.getElementById('submit');
submit.addEventListener('click', show);
uj5u.com熱心網友回復:
單擊顯示按鈕時訪問輸入值,以便獲得用戶輸入的更新值。
const show = () => {
const userTextValue = document.getElementById('user-text').value;
console.log(userTextValue);
}
const submit = document.getElementById('submit');
submit.addEventListener('click', show);
<div>
<input id="user-text" />
<button id="submit">Show</button>
</div>
uj5u.com熱心網友回復:
當您默認提交頁面重繪 以防止您可以這樣做
const show = (e) =>{
e.preventDefault();
console.log(userTextValue);
}
uj5u.com熱心網友回復:
該函式必須有一個定義的引數。
最好設定 userTextValue 變數不帶 .value,您將在呼叫 show() 函式時宣告它。
const userTextValue = document.getElementById('user-text');
const submit = document.getElementById('submit');
submit.addEventListener('click', () => {
show(userTextValue.value);
});
const show = (value) => {
console.log(value);
};
<input type="text" id="user-text">
<input type="submit" id="submit">
希望對你有幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/427025.html
標籤:javascript html dom
上一篇:C ConcurrencyInAction:7.2.2阻止那些討厭的泄漏:在無鎖資料結構中管理記憶體
下一篇:如何更改附加字串的顏色?
