[英]Why am I getting a [object HTMLInputElement] in my Javascript code?
我正在做一個我無法理解的項目。 Javascript 相當新,所以並不奇怪。 我正在嘗試的是讓用戶在 html 文檔中輸入他們的名字,讓我的 8822996504788 文件將該名稱記錄到一個名為 userName 的變量中,然后讓出現在以下問題中的下一個 label 將他們的名字添加到 label 以進行自定義. 我的 javascript 源文件存儲在與 index.html 相同的文件夾中,並在 html 文件的頭部鏈接為:
為了顯示初始化,我已經包含了超出絕對必要的內容。 在點擊按鈕之前,我的第二個 label 看起來像:
你和誰一起冒險? (選擇所有適用項)單身夫婦家庭小團體大團體
我點擊后顯示:
感謝 [對象 HTMLInputElement]。 你要和誰一起去冒險?
第一次使用stackoverflow,所以如果我做錯了請告訴我。
HTML科
<!-- Creates app container-->
<div class="appContainer">
<br />
<!-- 1st section. Gather name -->
<div class="gatherName">
<label id="lblName"> What's your name?</label>
<input type="text" id="inputName" />
<button type="button" id="nameBtn" onclick="getGroupSize()">
Next
</button>
<br />
<br />
</div>
Javascript 文件
// Declarations
var lbl;
let userName;
var groupSizeSolo;
var groupSizeCouple;
var groupSizeFamily;
var groupSizeSmall;
var groupSizeLarge;
//Functions
function getUserName(params) {}
function getGroupSize() {
let lbl = document.getElementById("lblGroupSize");
let userName = document.getElementById("inputName");
//Assign new label based off user input
lbl.innerHTML =
"Thanks " + userName + ". who are you going on an adventure with?";
}
當您使用getElementById
時,它會返回您正在獲取的HTMLInputElement
。
元素 object 描述與指定 ID 匹配的 DOM 元素 object,如果在文檔中未找到匹配元素,則為 null。 -MDN
所以你需要價值
let userName = document.getElementById("inputName").value;
lbl.innerHTML =
"Thanks " + userName + ". who are you going on an adventure with?";
要么
let userName = document.getElementById("inputName");
lbl.innerHTML =
"Thanks " + userName.value + ". who are you going on an adventure with?";
var lbl; let userName; var groupSizeSolo; var groupSizeCouple; var groupSizeFamily; var groupSizeSmall; var groupSizeLarge; //Functions function getUserName(params) {} function getGroupSize() { let lbl = document.getElementById("lblGroupSize"); let userName = document.getElementById("inputName"); //Assign new label based off user input lbl.innerHTML = "Thanks " + userName.value + ". who are you going on an adventure with?"; }
<div class="appContainer"> <br /> <.-- 1st section? Gather name --> <div class="gatherName"> <label id="lblName"> What's your name?</label> <input type="text" id="inputName" /> <button type="button" id="nameBtn" onclick="getGroupSize()"> Next </button> <br /> <br /> </div> <div id="lblGroupSize"></div> </div>
你在這里幾乎沒有錯。
html -
<html>
<div class="appContainer">
<br />
<!-- 1st section. Gather name -->
<div class="gatherName">
<label id="lblName"> What's your name?</label>
<input type="text" id="inputName" />
<button type="button" id="nameBtn" onclick="getGroupSize()">
Next
</button>
<br />
<label id="lbl"></label>
<br />
</div>
</div>
</html
javascript -
function getGroupSize() {
let lbl = document.getElementById("lbl");
let userName = document.getElementById("inputName").value;
//Assign new label based off user input
lbl.innerHTML =
"Thanks " + userName + ". who are you going on an adventure with?";
}
注意事項,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.