我有一个带有这样复选框的表单:
<input type="checkbox" name="type[]" value="1" />Fast Food<br>
<input type="checkbox" name="type[]" value="2" />Table Service<br>
<input type="checkbox" name="type[]" value="3" />Cafeteria<br>
当我在名称中使用括号(类型 [])时,我的 php 工作:
$type=$_POST['type'];
echo "types are:";
for ( $counter = 0; $counter < sizeof($type); $counter += 1) {
echo "<br>".$type[$counter];
}
但我的 javascript 不起作用:
var f = document.addform;
for (var i=0;i<f.type.length;i++){
if(f.type[i].checked==true){
break;
}
if(i==(f.type.length-1)){
alert("No categories entered!");
valid=false;
}
}
但是,如果我去掉括号:
<input type="checkbox" name="type" value="1" />Fast Food<br>
然后 PHP 不起作用,但 javascript 起作用。
这里发生了什么?我应该用什么?
谢谢。