简体   繁体   English

我无法保存输入(HTML 和 JS)

[英]I can't save a input (HTML and JS)

Respected friends,尊敬的朋友们,

I'm a student of web devel, and self-taught.我是web devel的学生,自学成才。 Also, I'm learning about DOM with Javascript. I am making a simple formulary, with the objective to play with positions, change colors and managing the DOM, etc. I want to create some participators, and display them in a list.此外,我正在使用 Javascript 学习 DOM。我正在制作一个简单的公式,目的是玩位置、更改 colors 和管理 DOM 等。我想创建一些参与者,并将它们显示在列表中。

This is the part of my HTML code.这是我的 HTML 代码的一部分。

<label>Nombre: </label>
<input id="addNombre" type="text"/>
<label>Color :</label>
<input id="addNombre" type="text"/>
<button onclick="AddCompetidor()">Añade Participante</button>

I have done and redone many JS functions, this is the last one I have done.我已经完成并重做了很多 JS 功能,这是我完成的最后一个。

function AddCompetidor(){
    var nombre = document.getElementById("addNombre");
    var color = document.getElementById("addColor");
    alert (nombre,color);
}

I tried with others functions, how to create an array, but I don't know if it's the best way to approach it.我尝试了其他函数,如何创建数组,但我不知道这是否是处理它的最佳方法。

Thanks a lot!!非常感谢!!

Here is the comprehensive solution with comments.这是带有评论的综合解决方案。

 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>

If you want to create an array of HTML input values, use:如果要创建 HTML 输入值的数组,请使用:

var array = [nombre.value, color.value]

The square brackets define a new array, and getting the value parameter gets what's inside the <input> tag.方括号定义了一个新数组,获取value参数就获取了<input>标签内的内容。

you have to get the correct id of element:您必须获得正确的元素 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>

You can define a class你可以定义一个 class

class players{ class 玩家{

constructor( nombre , color){ 
         this.nombre = nombre; 
         this.color = color;   

}} and create as many object and add each object in a array like this }} 并创建尽可能多的 object 并将每个 object 添加到这样的数组中

var obj = new players( nombarvalue, colorvalue); var obj = new players(nombarvalue, colorvalue);

var list = [];变量列表 = [];

list.push(obj); list.push(obj);

then iterate this array of object to print a list然后迭代这个 object 的数组来打印一个列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM