<form action="test.php" method="post" onSubmit="return checkCheckBoxes(this);">
<input type="CHECKBOX" name="CHECKBOX_1" value="1">y
<input type="CHECKBOX" name="CHECKBOX_2" value="2">o
<input type="CHECKBOX" name="CHECKBOX_3" value="3">t
<input type="SUBMIT" value="Submit!">
</form>
这是Javascript代码
<script type="text/javascript" language="javascript">
function checkCheckBoxes(theForm) {
if (
theForm.CHECKBOX_1.checked == false &&
theForm.CHECKBOX_2.checked == false &&
theForm.CHECKBOX_3.checked == false)
{
alert ('Please make sure to check a checkboxe!');
return false;
} else {
return true;
}
}
</script>
答案 0 :(得分:1)
亲爱的首先,您需要将复选框的名称更改为任何类似的数组
<form action="test.php" method="post" onSubmit="return checkCheckBoxes(this);"> <input type="CHECKBOX" name="CHECKBOX[]" value="1">y <input type="CHECKBOX" name="CHECKBOX[]" value="2">o <input type="CHECKBOX" name="CHECKBOX[]" value="3">t <input type="SUBMIT" value="Submit!"> </form>
在你的test.php上,只需遍历整个CHECKBOX阵列
for($_POST['CHECKBOX'] as $key=>$value) { if(isset($key)) echo('check box is checked and do some thing with '.$value); else echo('check box is not checked'); }
答案 1 :(得分:0)
<script type="text/javascript" language="javascript">
function checkCheckBoxes(theForm) {
if(theForm.CHECKBOX_1.checked == false &&
theForm.CHECKBOX_2.checked == false &&
theForm.CHECKBOX_3.checked == false )
{
alert ('Please make sure to check a checkboxe!');
return false;
} else {
var val=''; var arr = new Array();
if(theForm.CHECKBOX_1.checked == true){
val += theForm.CHECKBOX_1.value; arr.push(/*push val*/);
}
if(theForm.CHECKBOX_2.checked == true){
val += theForm.CHECKBOX_2.value;
}
if(theForm.CHECKBOX_3.checked == true){
val += theForm.CHECKBOX_3.value;
}
alert(val); //echo out array elements
}
}
</script>
<form action="test.php" method="post" onSubmit="return checkCheckBoxes(this);">
<input type="CHECKBOX" name="CHECKBOX_1" value="1"/>y
<input type="CHECKBOX" name="CHECKBOX_2" value="2"/>o
<input type="CHECKBOX" name="CHECKBOX_3" value="3"/>t
<input type="SUBMIT" value="Submit!">
</form>
<强> [UPDATE] :: 强>
<?php
for($_POST['CHECKBOX'] as $key=>$value)
{
if(isset($key))
echo "var val[".++$count."] = '".$value."'";
}
?>
<script type="text/javascript">
for(var i=0; i<val.length; i++)
alert(val[i]);
</script>