获取选中和未选中复选框的值 [英] Get checked and unchecked checkboxes value
问题描述
这是我的脚本:
HTML代码:
<脚本>
函数pickIt(pId){
if(document.getElementById(pId).checked == true){
document.getElementById(pId).checked = false;
} else {
document.getElementById(pId).checked = true;
}
submitForm();
返回true;
}
< / script>
< img src =images / bagua-square.gifborder =0usemap =#Map2/>
< map name =Map2id =Map2>
< / map>
< div style =display:none;>
< input type =checkboxid =1name =box []/>
< input type =checkboxid =2name =box []/>
< input type =checkboxid =3name =box []/>
< / div>
PHP代码:
<?php
print_r($ _ POST ['box']);
?>
当我点击box id 1 pickIt()函数打开复选框1到打开。并且php显示数组(0 =>'on')
但是我也想获得checkbox值,这些值不会被检查,因此php会显示
数组(0 =>'on',1 =>'off',2 =>'off')
实际上我想获得所有复选框的状态在和关闭上,因为我在mysql db中使用这些id来更新记录状态 或关闭。
请指导。
复选框在检查时发送它们的值,如果它们没有发送
您应该有:
< input type =复选框id =1name =box []value =1/>
< input type =checkboxid =2name =box []value =2/>
< input type =checkboxid =3name =box []value =3/>
所以这些值将是1,2和3而不是on,on和on。然后你可以知道哪些被检查,因为它们不会全部相同。
如果你真的希望你的数据结构是 array( 0 =>'on',1 =>'off',2 =>'off')
然后你可以这样做:
$ foo = array();
for($ i = 1; $ i <= 3; $ i ++){
$ foo [$ i] = in_array($ i,$ _GET ['box'])? '开关';
}
This is my script:
HTML Code:
<script>
function pickIt(pId){
if(document.getElementById(pId).checked==true){
document.getElementById(pId).checked=false;
}else{
document.getElementById(pId).checked = true;
}
submitForm();
return true;
}
</script>
<img src="images/bagua-square.gif" border="0" usemap="#Map2" />
<map name="Map2" id="Map2">
<area shape="rect" coords="201,14,284,100" onclick="pickIt(1);" />
<area shape="rect" coords="202,104,284,190" onclick="pickIt(2);" />
<area shape="rect" coords="202,195,284,283" onclick="pickIt(3);" />
</map>
<div style="display:none;">
<input type="checkbox" id="1" name="box[]" />
<input type="checkbox" id="2" name="box[]" />
<input type="checkbox" id="3" name="box[]" />
</div>
PHP Code:
<?php
print_r($_POST['box']);
?>
When I click on box id 1 pickIt() function turn checkbox 1 to on. And the php shows array(0=>'on')
But I also want to get checkbox value which are not checked such that php will show array(0=>'on', 1=>'off', 2=>'off')
Actually i want to get all checkboxes with their status on and off because i am using these id in mysql db to update record status on or off. please guide.
Checkboxes send their value if they are checked and are not sent at all if they are not.
You should have:
<input type="checkbox" id="1" name="box[]" value="1" />
<input type="checkbox" id="2" name="box[]" value="2" />
<input type="checkbox" id="3" name="box[]" value="3" />
So the values will be 1, 2 and 3 instead of on, on and on. Then you can tell which ones are checked as they won't all be the same.
If you really want your data structure to be array(0=>'on', 1=>'off', 2=>'off')
then you could do:
$foo = array();
for ($i = 1; $i <= 3; $i++) {
$foo[$i] = in_array($i, $_GET['box']) ? 'on' : 'off';
}
这篇关于获取选中和未选中复选框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!