用js验证radio单选按钮是否选,以及验证checkbox复选框是否选中。
代码:
<html>
<head>
<title>JS验证radio和checkbox是否选中-www.jb200.com</title>
<script src="chk.js"></script>
</head>
<body>
<form id="form" name="form" method="post" action="http://www.jb200.com" onsubmit="return CheckForm()">
<input type="checkbox" name="love" >
<input type="checkbox" name="love" >
<input type="checkbox" name="love" >
<input type="checkbox" name="love" >
<input type="radio" value="好的" name="miaoshu" >
<input type="radio" value="好的" name="miaoshu" >
<input type="radio" value="好的" name="miaoshu" >
<input id="txtbox" name="MyTextfield" type="text">
<input type="submit" value="提交" >
</form>
</body>
</html>
2,验证代码 chk.js
<script>
/**
* 验证radio单选按钮与checkbox复选框是否选中
* by www.jb200.com
*/
function CheckForm() {
var flag = false;
var allRadio = document.form.elements["miaoshu"];
var txtBox = document.getElementById("txtBox").value;
for (var i = 0; i < allRadio.length; i++) {
if (allRadio[i].checked) {
if (allRadio[allRadio.length - 1].checked) {
if (txtBox == "") {
flag = false;
}
else {
flag = true;
}
}
}
}
if (!flag) {
alert("请确定输入完整");
}
var cbxs = document.getElementsByName('love');
var s = 0;
for (var i = 0; i < cbxs.length; i++) {
if (cbxs[i].checked)
{ s += 1; }
}
if (s < 1) {
alert("请确定输入完整");
flag = false;
}
return flag;
}
</script>
您可能感兴趣的文章:
js 检验(input、radio、checkbox、textarea)必填项的代码
js判断单选框(radio)是否被选中的例子
js判断radio是否被选中的简单例子
JS动态判断radio是否选中的方法
radio和checkbox及php select默认选择的实现代码
js实现radio和select的默认选中
radio实现checkbox效果 再次点击取消选择