简体   繁体   English

Js - 遍历数组以创建仅填充最后一项输入值的输入和值

[英]Js - Loop through array to create inputs and values only filling last item input value

I have a div with id="inputs" on html and the following code on js:我在 html 上有一个 id="inputs" 的 div,在 js 上有以下代码:

let paises=[
    {pais: "Honduras",scripttag:`<script src="perro loco come baba"> 'cha cha'` },
    {pais: "Chile",scripttag:"perropapa"},
    {pais: "Madagascar",scripttag:"otro"}
]
let inputDiv=document.getElementById("inputs")
for(let p of paises){
    if(p.scripttag){
        inputDiv.innerHTML+=`<input disabled id="` + p.pais + `">`
        let inputPais=document.getElementById(p.pais)
        inputPais.value=p.scripttag
    }
}

If the element of the paises array has a scripttag property, an input is created and value is filled with scripttag content.如果 paises 数组的元素具有 scripttag 属性,则会创建一个输入并用 scripttag 内容填充值。

Inputs get created properly, the issue is that in the page all the inputs are empty except the last one created (on this case the input with id Madagascar is filled with "otro")输入被正确创建,问题是页面中的所有输入都是空的,除了最后一个创建的输入(在这种情况下,id为马达加斯加的输入用“otro”填充)

Your issue is that you are not creating a new element every time you want to add a new input.您的问题是您不是每次要添加新输入时都创建新元素。

Instead of adding html to divs like that, you can make use of createElement .您可以使用createElement ,而不是将 html 添加到这样的 div 中。

let paises=[
    {pais: "Honduras",scripttag:`<script src="perro loco come baba"> 'cha cha'` },
    {pais: "Chile",scripttag:"perropapa"},
    {pais: "Madagascar",scripttag:"otro"}
]
let inputDiv=document.getElementById("inputs")
for(let p of paises){
    if(p.scripttag){
        const newInput = document.createElement('input')
        newInput.id = p.pais
        newInput.value = p.scripttag
        inputDiv.appendChild(newInput)
    }
}

This way you are adding a new element to the div each loop.这样,您将在每个循环的 div 中添加一个新元素。 Instead of overwriting the previous element.而不是覆盖前一个元素。 I also think you have more control over the inputs properties using DOM manipulation instead writing everything out in a string and concating.我还认为您可以使用 DOM 操作来更好地控制输入属性,而不是将所有内容都写在字符串中并进行连接。

Another option is that you could just set the value with the HTML you are generating.另一种选择是您可以使用您正在生成的 HTML 设置该值。

 let paises=[ {pais: "Honduras",scripttag:`<script src='perro loco come baba'/> 'cha cha'` }, {pais: "Chile",scripttag:"perropapa"}, {pais: "Madagascar",scripttag:"otro"} ] let inputDiv=document.getElementById("inputs") for(let p of paises){ if(p.scripttag){ inputDiv.innerHTML+=`<input disabled id="${p.pais}" value="${p.scripttag}">` } }
 <div id='inputs'></div>

 let paises=[ {pais: "Honduras",scripttag:`<script src="perro loco come baba"> 'cha cha'` }, {pais: "Chile",scripttag:"perropapa"}, {pais: "Madagascar",scripttag:"otro"} ] let inputDiv=document.getElementById("inputs") for(let p of paises){ if(p.scripttag){ inputDiv.innerHTML= inputDiv.innerHTML +`<input disabled='true' id="` + p.pais + `" value='${p. scripttag}'/>` } }
 <div id="inputs"></div>

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

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