javascript 获取选中和未选中的复选框值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9807214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:54:52  来源:igfitidea点击:

Get checked and unchecked checkboxes value

phpjavascriptmysqlhtml

提问by PHP Ferrari

This is my script:

这是我的脚本:

HTML Code:

HTML代码:

<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代码:

<?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')

当我单击 box id 1 pickIt()函数时,将复选框 1 设置为on。并且 php 显示 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')

但我也想获取未选中的复选框值,以便 php 显示数组(0=>'on', 1=>'off', 2=>'off')

Actually i want to get all checkboxes with their status onand offbecause i am using these id in mysql db to update record status onor off. please guide.

实际上,我想让所有复选框都处于打开关闭状态因为我在 mysql db 中使用这些 id 来打开关闭更新记录状态。请指导。

回答by Quentin

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.

因此,值将是 1、2 和 3,而不是 on、on 和 on。然后你就可以知道哪些被检查了,因为它们不会都一样。

If you really want your data structure to be array(0=>'on', 1=>'off', 2=>'off')then you could do:

如果你真的想要你的数据结构,array(0=>'on', 1=>'off', 2=>'off')那么你可以这样做:

$foo = array();
for ($i = 1; $i <= 3; $i++) {
    $foo[$i] = in_array($i, $_GET['box']) ? 'on' : 'off';
}

回答by Nicola Peluchetti

A checkbox will be sent to the server (when the form is submitted) only if it's checked, otherwise it's not submitted

一个复选框只有在被选中时才会被发送到服务器(当表单被提交时),否则它不会被提交

also change

也变了

onclick="pickIt('1');" /

回答by Frank van Wijk

Two options:

两种选择:

  1. Make hidden fields per checkbox and set the value to 0 or 1 with javascript when you (un)check the checkbox. Then ignore the "on" values in your $_POST.
  2. Php must know how many (and which) checkboxes there are, so the $_POSTvalues that are missing are unchecked boxes. You could do this with a for loop.
  1. 为每个复选框设置隐藏字段,并在您(取消)选中复选框时使用 javascript 将值设置为 0 或 1。然后忽略$_POST.
  2. Php 必须知道有多少(和哪些)复选框,因此$_POST缺少的值是未选中的框。你可以用 for 循环来做到这一点。