JS判断radio是否为空
代码:
<html>
<head>
<script type="text/javascript">
function Alert()
{
var a =0;
var n = document.getElementById("name").value;
let type = document.getElementsByName("usertype");
let value;
for (let index in type) {
if (type[index].checked) {
value = type[index].value;
break;
}
}
if(n=="")
return "请输入姓名!!!";
if (value == null)
return "请选择类型!!!";
}
</script>
</head>
<body>
<input id="name" type="text" placeholder="请输入姓名"><br>
<input type="radio" name="usertype" value="student" id="student">学生<br>
<input type="radio" name="usertype" value="teacher" id="teacher">老师<br>
<input type="radio" name="usertype" value="administrator" id="administrator">管理员<br>
<button value="submit" onclick="document.getElementById('alert').innerHTML=Alert()">提交</button>
<p id="alert" style="color:red"></p>
<br>
</body>
</html>
结果: