php让checkbox复选框初始选中,很常用的功能,备忘下:


代码如下:

<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/><?php$arr=array(0=>array('id'=>100,'title'=>'京东'),1=>array('id'=>200,'title'=>'天猫'),2=>array('id'=>300,'title'=>'苏宁'));$arrId=array(100,300);if(isset($_POST['mall'])){echo'<pre/>';print_r($_POST['mall']);exit();}?><formaction=""method="post"><?phpforeach($arras$key=>$value){?><?phpif(in_array($value['id'],$arrId)){?><inputtype="checkbox"name="mall[]"value="<?phpecho$value['id']?>"checked><?phpecho$value['title']?><?php}else{?><inputtype="checkbox"name="mall[]"value="<?phpecho$value['id']?>"><?phpecho$value['title']?><?php}?><?php}?><inputtype="submit"value="提交"/></form>