日期:2014-05-16  浏览次数:20391 次

用 Javascript 验证表单(form)中多选框(checkbox)的值
<form name="form1" method="post" action="">
<p>
<input name="checkbox" type="checkbox" value="汗珠子" checked>net
<input name="checkbox" type="checkbox"  value="汉字">wps
</p>
<input name="nnn" type="button" value="点此获取值" onClick="alert(getCheckboxValue(this.form.elements['checkbox']))">
<input type="button" name="Submit2" value="获取值"onclick="myclick(s30c_tbl0_xja_男_女)">
<input name="s30c_tbl0_xja_男_女" type="hidden" value="">
<p>&nbsp;  </p>
<p>&nbsp;</p>
</form>
<script type="text/javascript"> // 说明: 用 Javascript 验证表单(form)中多选框(checkbox)的值 // 作者: CodeBit.cn ( http://www.CodeBit.cn )
function myclick(obj) {
alert(obj.value);
}
function getCheckboxValue(checkbox) {
alert(checkbox.length);
if (!checkbox.length && checkbox.type.toLowerCase() == 'checkbox')
{
return (checkbox.checked)?checkbox.value+"---":'';
}
if (checkbox[0].tagName.toLowerCase() != 'input' ||checkbox[0].type.toLowerCase() != 'checkbox')
{ return '';
}
var val = [];
var len = checkbox.length;
for(i=0; i<len; i++) {
if (checkbox[i].checked)
{
val[val.length] = checkbox[i].value;
}
}
var str=(val.length)?val:'';
document.all['s30c_tbl0_xja_男_女'].value=str;
return str;
}
</script>