我想讓用戶提交他們的名字,而名字將顯示在選項上。
HTML
<form action="#"/span>>/span>
< input type="text" id="p1Name">>
< input type="text" id="p2Name">
<button id="start"/span>> Start</button>。
</form>/span>
<label for="selected"/span>> 選擇播放器</label>
<select name="" id="selected">/span>
<option value="p1"/span>> <span id="select1"/span>> 玩家1</span></option>/所以 "玩家1 "和 "玩家2 "應該用用戶的名字替換。
<option value="p2"> < span id="select2">Player 2< /span></option>/span>
</select>/span>
Javascript
const p1 = {
displaySelect: document.querySelector('#select1')
}
const p2 = {
displaySelect: document.querySelector('#select1')
}
const startButton = document.querySelector('#start')。
startButton.addEventListener('click', () => {
讓nameOne = document.getElementById('p1Name').value。
let nameTwo = document.getElementById('p2Name').value;
p1.displaySelect.textContent = nameOne;
p2.displaySelect.textContent = nameTwo;
})
似乎p1.displaySelect是空的。我試著在網上查了一下,但我真的找不到。請看一下我的代碼。
app.js:26 Uncaught TypeError: 不能設定屬性of null(設定'innerHTML')。
在HTMLButtonElement.<anonymous>
uj5u.com熱心網友回復:
你不應該把你的id放在option標簽里面。相反,它應該在option標簽本身。
下面代碼中的preventDefault方法有助于阻止HTML對你的重定向。
希望這有幫助
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/320438.html
標籤:
