Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用“全选验证”删除选中的复选框项_Php_Javascript - Fatal编程技术网

Php 使用“全选验证”删除选中的复选框项

Php 使用“全选验证”删除选中的复选框项,php,javascript,Php,Javascript,我有一个项目列表,生成为: <input type="checkbox" name="sacb" onchange="selAll();" title="select all"/> <?php while($row = mysql_fetch_array($res)){ echo "<div><input type='checkbox' name='cb' value='$row[id]'>$row[item]</div>";

我有一个项目列表,生成为:

<input type="checkbox" name="sacb" onchange="selAll();" title="select all"/>
  <?php
  while($row = mysql_fetch_array($res)){
    echo "<div><input type='checkbox' name='cb' value='$row[id]'>$row[item]</div>";
  }
?>


不能使用点表示法访问名称中带有括号的元素,因此必须使用elements集合检索它们

function selAll(){
    var s = document.form.sacb;
    var checkboxes = document.form.elements['cb[]'];
    for (var i = 0; i < checkboxes.length; i++) {
        checkboxes[i].checked = s.checked;
    }
}
函数seall(){
var s=document.form.sacb;
var复选框=document.form.elements['cb[]'];
对于(变量i=0;i
您可以从下面的javascript获取更新所选复选框

请添加您的代码以选择要获取简单javascript的数组数 在这个例子中,我使用jquery只获取复选框数组,并检查它是否被选中

var field, fields = $('.edit_user_notifi :checkbox'), i = fields.length;
        var notification_status = '';
        while (i--) {
            field = fields[i];
            if(notification_status == '')
            {
                if (field.checked) {
                    notification_status = field.id+'~1';

                } else {
                    notification_status = field.id+'~0';
                }
            }else{

                if (field.checked) {
                    notification_status = notification_status +','+ field.id+'~1';
                }else{
                    notification_status = notification_status +','+ field.id+'~0';
                }

            }
        }
只需更改一些逻辑即可使用简单的javascript,或者如果您使用jquery的话,也可以更改一些逻辑

function selAll(){
    var s = document.form.sacb;
    var checkboxes = document.form.elements['cb[]'];
    for (var i = 0; i < checkboxes.length; i++) {
        checkboxes[i].checked = s.checked;
    }
}
var field, fields = $('.edit_user_notifi :checkbox'), i = fields.length;
        var notification_status = '';
        while (i--) {
            field = fields[i];
            if(notification_status == '')
            {
                if (field.checked) {
                    notification_status = field.id+'~1';

                } else {
                    notification_status = field.id+'~0';
                }
            }else{

                if (field.checked) {
                    notification_status = notification_status +','+ field.id+'~1';
                }else{
                    notification_status = notification_status +','+ field.id+'~0';
                }

            }
        }