回显选中的复选框可以通过以下步骤实现:
以下是一个示例代码,演示如何回显选中的复选框:
<!DOCTYPE html>
<html>
<head>
<title>回显选中的复选框</title>
</head>
<body>
<h3>选择你喜欢的水果:</h3>
<input type="checkbox" id="apple" name="fruit" value="apple">苹果<br>
<input type="checkbox" id="banana" name="fruit" value="banana">香蕉<br>
<input type="checkbox" id="orange" name="fruit" value="orange">橙子<br>
<input type="checkbox" id="grape" name="fruit" value="grape">葡萄<br>
<br>
<button onclick="showSelectedFruits()">显示选中的水果</button>
<script>
function showSelectedFruits() {
var checkboxes = document.getElementsByName("fruit");
var selectedFruits = [];
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
selectedFruits.push(checkboxes[i].value);
}
}
alert("你选中的水果是:" + selectedFruits.join(", "));
}
</script>
</body>
</html>
在上述示例中,我们通过使用document.getElementsByName("fruit")
获取了所有name属性为"fruit"的复选框元素。然后,我们遍历这些复选框元素,检查每个复选框的checked属性是否为true,如果是,则将其值添加到selectedFruits数组中。最后,我们使用alert()
方法显示选中的水果。
对于腾讯云相关产品和产品介绍链接地址,由于要求不能提及具体品牌商,建议您参考腾讯云的官方文档或者搜索相关的云计算服务提供商的文档来了解相关产品和服务。
领取专属 10元无门槛券
手把手带您无忧上云