4 回答

TA貢獻2080條經驗 獲得超4個贊
這是帶有評論的綜合解決方案。
const form = document.getElementById("form");
const list = document.getElementById("list");
// let's handle the form submit event
form.addEventListener("submit", (e) => {
// this will prevent the form from doing network request
e.preventDefault();
// lets get input elements from form by their ids
const { nombre, color } = e.target.elements;
// lets add new elements in the list
const li = document.createElement("li");
li.innerHTML = `${nombre.value} — ${color.value}`;
list.appendChild(li);
// lets clear the form
nombre.value = "";
color.value = "";
});
<form id="form">
<label>Nombre: </label>
<input id="nombre" type="text" />
<label>Color :</label>
<input id="color" type="text" />
<button>A?ade Participante</button>
</form>
<ul id="list"></ul>

TA貢獻2051條經驗 獲得超10個贊
你必須獲得元素的正確ID:
<label>Nombre: </label>
<input id="nombre" type="text"/>
<label>Color :</label>
<input id="color" type="text"/>
<button onclick="AddCompetidor()">A?ade Participante</button>
<script type="text/javascript">
function AddCompetidor(){
var nombre = document.getElementById("nombre").value;
var color = document.getElementById("color").value;
alert (nombre + color);
}
</script>

TA貢獻1993條經驗 獲得超6個贊
如果要創建 HTML 輸入值數組,請使用:
var array = [nombre.value, color.value]
方括號定義一個新數組,獲取value
參數即可獲取標簽內的內容<input>
。

TA貢獻1836條經驗 獲得超5個贊
你可以定義一個類
班級球員{
constructor( nombre , color){ this.nombre = nombre; this.color = color;
并創建盡可能多的對象并將每個對象添加到數組中,如下所示
var obj = 新玩家(nombarvalue, colorvalue);
var 列表 = [];
列表.push(obj);
然后迭代這個對象數組來打印一個列表
添加回答
舉報