该例子允许你选择一系列同名的 checkbox ,并给出您选择的值。
<html>
<head>
<script type="text/javascript">
function getId()
{
var id_list = "";
var x = document.getElementsByName("aid");
for (i=0; i<x.length; i++) {
if (x[i].checked == true) {
id_list += ","+x[i].value;
}
}
if(id_list == "" ){
alert("您没有做任何选择!")
}else{
alert(id_list.substr(1));
}
}
</script>
</head>
<body>
<p>请选择要删除的数据:
<input type="checkbox" name="aid" value="1" />1
<input type="checkbox" name="aid" value="2" />2
<input type="checkbox" name="aid" value="3" />3
<input type="checkbox" name="aid" value="4" />4
<input type="checkbox" name="aid" value="5" />5
<button onclick="getId()">确定</button>
</p>
</body>
</html>
运行该例子,选择任意 checkbox ,如 2,3,5,点击确认按钮,会弹出提示框并打印出 2,3,5。