简体   繁体   English

在特定位置插入多个元素

[英]Insert multiple elements on specific positions

I'm trying to insert multiple elements on specific positions.我正在尝试在特定位置插入多个元素。

In this exemple (see under) I want to insert ItemA1 always on first place and Item A2 always on second place and so on.在这个例子中(见下文),我想始终将 ItemA1 插入第一位,将 ItemA2 始终插入第二位,依此类推。 The probleme is that with the method parent.insertBefore(newSpan, parent.children[number]), it doesn't work if you check the radios from the end:/问题是使用方法 parent.insertBefore(newSpan, parent.children[number]),如果你从最后检查收音机,它就不起作用:/

Is there a way to "give an order" to elements that are created?有没有办法对创建的元素“下订单”?

Thanks for any help:)谢谢你的帮助:)

 function createItem(x,y){ let result = document.getElementById(x+"Rap") if(typeof(result) == 'undefined' || result == null){ let newSpan = document.createElement("span"); newSpan.setAttribute("id",x+"Rap") let newText = document.createTextNode("."); newSpan.appendChild(newText); document.getElementById("result").insertBefore(newSpan, document.getElementById("result").children[y]); } } function generateItem(x,y){ createItem(x,y); if (document.getElementById("NT"+x).checked === true){ document.getElementById(x+"Rap").remove(); } else{ document.getElementById(x+"Rap").innerHTML = document.querySelector("input[name="+x+"]:checked").value; } }
 #result{ margin-top: 10px; padding: 5px; border: solid black 1px; }
 <div> <div>itemA1 <input id="yesA1" name="A1" type="radio" value="item A1 = yes, " onchange="generateItem('A1',0)"/> <label for="yesA1">yes</label> <input id="noA1" name="A1" type="radio" value="item A1 = no, " onchange="generateItem('A1',0)"/> <label for="noA1">no</label> <input id="NTA1" name="A1" type="radio" value="item A1 = NT, " checked="checked" onchange="generateItem('A1',0)"/> <label for="NTA1">NT</label> </div> <div>itemA2 <input id="yesA2" name="A2" type="radio" value="item A2 = yes, " onchange="generateItem('A2',1)"/> <label for="yesA2">yes</label> <input id="noA2" name="A2" type="radio" value="item A2 = no, " onchange="generateItem('A2',1)"/> <label for="noA2">no</label> <input id="NTA2" name="A2" type="radio" value="item A2 = NT, " checked="checked" onchange="generateItem('A2',1)"/> <label for="NTA2">NT</label> </div> <div>itemB1 <input id="yesB1" name="B1" type="radio" value="item B1 = yes, " onchange="generateItem('B1',2)"/> <label for="yesB1">yes</label> <input id="noB1" name="B1" type="radio" value="item B1 = no, " onchange="generateItem('B1',2)"/> <label for="noB1">no</label> <input id="NTB1" name="B1" type="radio" value="item B1 = NT, " checked="checked" onchange="generateItem('B1',2)"/> <label for="NTB1">NT</label> </div> <div>itemB2 <input id="yesB2" name="B2" type="radio" value="item B2 = yes." onchange="generateItem('B2',3)"/> <label for="yesB2">yes</label> <input id="noB2" name="B2" type="radio" value="item B2 = no." onchange="generateItem('B2',3)"/> <label for="noB2">no</label> <input id="NTB2" name="B2" type="radio" value="item B2 = NT." checked="checked" onchange="generateItem('B2',3)"/> <label for="NTB2">NT</label> </div> <div id="result">Results: </div> </div>

This was mentioned in a comment by Kinglish , but here is an example of how you could loop through all of the radios and create your result output line any time a radio is clicked. Kinglish在评论中提到了这一点,但这里是一个示例,说明如何循环遍历所有无线电并在每次单击无线电时创建结果output 行。

 document.querySelectorAll("input[type='radio']").forEach(radio => { radio.addEventListener("click", e => { let result = [] document.querySelectorAll("input[type='radio']:checked").forEach(c => result.push(`item ${c.name} = ${c.value}`)) document.querySelector("#result").innerHTML = `Results: ${result.filter(f =>.f.includes("NT")),join(", ")}` }) })
 #result{ margin-top: 10px; padding: 5px; border: solid black 1px; }
 <div> <div>itemA1 <label><input name="A1" type="radio" value="yes">yes</label> <label><input name="A1" type="radio" value="no">no</label> <label><input name="A1" type="radio" value="NT" checked>NT</label> </div> <div>itemA2 <label><input name="A2" type="radio" value="yes">yes</label> <label><input name="A2" type="radio" value="no">no</label> <label><input name="A2" type="radio" value="NT" checked>NT</label> </div> <div>itemB1 <label><input name="B1" type="radio" value="yes">yes</label> <label><input name="B1" type="radio" value="no">no</label> <label><input name="B1" type="radio" value="NT" checked>NT</label> </div> <div>itemB2 <label><input name="B2" type="radio" value="yes">yes</label> <label><input name="B2" type="radio" value="no">no</label> <label><input name="B2" type="radio" value="NT" checked>NT</label> </div> <div id="result">Results:</div> </div>

Following the logic of what you seemed to have before, we loop through all of the radio buttons and attach a click event listener first.按照您之前似乎拥有的逻辑,我们遍历所有单选按钮并首先附加一个点击事件侦听器。 When clicked, it will then loop through all of the :checked radio buttons and get the name and value of those radio buttons.单击时,它将遍历所有:checked单选按钮并获取这些单选按钮的namevalue Lastly, it concatenates all of the values and displays them.最后,它连接所有值并显示它们。

Any checked radios with a value of 'NT' are ignored (which seems to currently be the case).任何值为“NT”的选中无线电都将被忽略(目前似乎是这种情况)。 Values are displayed in the order they appear on the page (which seems to fit the specific example you gave but may return mixed results if anything changes).值按照它们在页面上出现的顺序显示(这似乎符合您提供的特定示例,但如果有任何更改可能会返回混合结果)。

EDIT编辑

While the above works just fine, below is a modified version of the JavaScript that uses map() to get the output of all of the checked radio buttons, rather than build an array via forEach() .虽然上面的工作正常,但下面是 JavaScript 的修改版本,它使用map()获取所有选中的单选按钮的 output,而不是通过forEach()构建数组。

document.querySelectorAll("input[type='radio']").forEach(radio => {
  radio.addEventListener("click", e => {
    document.querySelector("#result").innerHTML = `Results : ${Array.from(document.querySelectorAll("input[type='radio']:checked")).map(m => `item ${m.name} = ${m.value}`).filter(f => !f.includes("NT")).join(", ")}`
  })
})

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

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