gpt4 book ai didi

javascript - JS 数组旁边的复选框

转载 作者:行者123 更新时间:2023-12-01 03:55:12 24 4
gpt4 key购买 nike

我有一个从数组中打印列表的函数。我希望用户能够通过使用旁边的复选框来选择数组中的多个不同项目。如何在每个数组元素旁边显示一个复选框? (正在学习JS)

function par() {
var courses = ["SDEV", "DBMS","INFM", "CSCI", "SVAD", "NETI", "ITSP", "CSIA"];
var text = "";
var i;
for (i = 0; i < courses.length; i++) {
text += courses[i] + "<br>";
}
document.getElementById("demo").innerHTML = text;
}

最佳答案

function par(domEl) {
var courses = ["SDEV", "DBMS","INFM", "CSCI", "SVAD", "NETI", "ITSP", "CSIA"];
var text = "";
var i;
for (i = 0; i < courses.length; i++) {
var checkBox = document.createElement("input");
checkBox.type = "checkbox";
checkBox.value = courses[i];
domEl.appendChild(checkBox);

var label = document.createElement("label");
label.innerText = courses[i];
domEl.appendChild(label);
}
}

var domEl = document.body.querySelector('#myDiv')
par(domEl);
<form>
<div id="myDiv"></div>
<input type="submit"/>
</form>

关于javascript - JS 数组旁边的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42822755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com