0

好的,所以我正在做的有几个与之相关的部分。

以下是涉及的步骤:

  1. 我使用 PHP 加载了一个复选框表(有一个包含 9 行(由 4 到 12 的 id 标识)和 3 列(用值 1 到 3 标识)的表)

  2. 加载页面后,复选框的状态需要更改。如果选中某些复选框,则需要禁用某些其他复选框。

  3. 我使用 jquery 更改功能来查看已选中或未选中的复选框。在这里,我根据用户选择使用与步骤 2 中相同的逻辑禁用或启用复选框。

  4. 每次在页面加载后选中或取消选中复选框时,我都会生成一个 JSON,将其推送到数组中并通过单击提交按钮将其发布到另一个页面。

所以我很擅长步骤 1,3 和 4。一旦页面加载,我需要帮助来更改复选框的状态。

一旦页面加载后复选框状态发生变化,如何获得与禁用复选框相同的逻辑?

这是我的 JavaScript:

$(document).ready(function(){
        var obj = {};
        var obj1 = {};
        var array = new Array();
        $("input[type='checkbox']").change(function() {

        if((this.id=='5' || this.id=='6' || this.id=='7' || this.id=='8' || this.id=='9' || this.id=='10' || this.id=='11' || this.id=='12') && this.value=='1') {
                var checkedId = this.id;
                var checkedOrNot = $(this).is(':checked');
                $("input[type='checkbox']").each(function() {
                    if(this.id==checkedId && (this.value=='2' || this.value=='3')) {
                        if(checkedOrNot) {
                            $(this).attr("disabled",true);
                        } else {
                            $(this).attr("disabled",false);
                        }
                    }
                });
            }



        if((this.id=='5' || this.id=='6' || this.id=='7' || this.id=='8' || this.id=='9' || this.id=='10' || this.id=='11' || this.id=='12') && this.value=='2') {
                var checkedId = this.id;
                var checkedOrNot = $(this).is(':checked');
                $("input[type='checkbox']").each(function() {
                    if(this.id==checkedId && (this.value=='1' || this.value=='3')) {
                        if(checkedOrNot) {
                            $(this).attr("disabled",true);
                        } else {
                            $(this).attr("disabled",false);
                        }
                    }
                });
            }



        if((this.id=='5' || this.id=='6' || this.id=='7' || this.id=='8' || this.id=='9' || this.id=='10' || this.id=='11' || this.id=='12') && this.value=='3') {
                var checkedId = this.id;
                var checkedOrNot = $(this).is(':checked');
                $("input[type='checkbox']").each(function() {
                    if(this.id==checkedId && (this.value=='1' || this.value=='2')) {
                        if(checkedOrNot) {
                            $(this).attr("disabled",true);
                        } else {
                            $(this).attr("disabled",false);
                        }
                    }
                });
            }



if(this.id=='4' && this.value=='1') {
    if($(this).is(':checked')) {
                $("input[type='checkbox']").each(function(){
            if(this.id=='4' && this.value=='1') {
            } else {
                $(this).attr("disabled",true);
            }
       });
    } else {
         $("input[type='checkbox']").each(function(){
            if(this.id=='4' && this.value=='1') {
            } else {
                $(this).attr("disabled",false);
            }
        });
    }
}


if(this.id=='4' && this.value=='2') {
    if($(this).is(':checked')) {
                $("input[type='checkbox']").each(function(){
            if(this.id=='4' && this.value=='2') {
            } else {
                $(this).attr("disabled",true);
            }
       });
    } else {
         $("input[type='checkbox']").each(function(){
            if(this.id=='4' && this.value=='2') {
            } else {
                $(this).attr("disabled",false);
            }
        });
    }
}


if(this.id=='4' && this.value=='3') {
    if($(this).is(':checked')) {
                $("input[type='checkbox']").each(function(){
            if(this.id=='4' && this.value=='3') {
            } else {
                $(this).attr("disabled",true);
            }
       });
    } else {
         $("input[type='checkbox']").each(function(){
            if(this.id=='4' && this.value=='3') {
            } else {
                $(this).attr("disabled",false);
            }
        });
    }
}
   //Other functionality
 });  });

更新

PHP表是这样的:

<?php
    foreach($a as $b) {
        if ($s["id"]== $b["c"]) {
            if($b["p"] == "e") {
                        echo '<td class="vcenter"><input type="checkbox" id="'.$s["id"].'" value="1" checked/></td>';
            }else {

                        echo '<td class="vcenter"><input type="checkbox" id="'.$s["id"].'" value="1"/></td>';
            }   
            if($b["p"] == "r") {
                        echo '<td class="vcenter"><input type="checkbox" id="'.$s["id"].'" value="2" checked/></td>';
            }else {
                        echo '<td class="vcenter"><input type="checkbox" id="'.$s["id"].'" value="2"/></td>';
            }
            if($b["p"] == "b") {
                        echo '<td class="vcenter"><input type="checkbox" id="'.$s["id"].'" value="3" checked/></td>';
            }else {
                        echo '<td class="vcenter"><input type="checkbox" id="'.$s["id"].'" value="3"/></td>';
            }
        }else {
            echo '<td class="vcenter"><input type="checkbox" id="'.$s["id"].'" value="1"/></td>';
            echo '<td class="vcenter"><input type="checkbox" id="'.$s["id"].'" value="2"/></td>';
            echo '<td class="vcenter"><input type="checkbox" id="'.$s["id"].'" value="3"/></td>';
        }
    }

?>

id 值从 4 到 12 不等。

条件如下:

如果选中第 5 到 12 行中的任何复选框,则应禁用同一行中的所有其他复选框。

如果选择了第一行中的任何复选框,则应禁用表中的所有其他复选框。

另一个更新

因此复选框将具有以下值:[id:value]

[4,1]    [4,2]  [4,3]
[5,1]    [5,2]  [5,3]
[6,1]    [6,2]  [6,3]
[7,1]    [7,2]  [7,3]
[8,1]    [8,2]  [8,3]
[9,1]    [9,2]  [9,3]
[10,1]   [10,2]  [10,3]
[11,1]   [11,2]  [11,3]
[12,1]   [12,2]  [12,3]

有多种可能性,因此您可以假设已选中任何复选框。但是所有的条件都应该成立。

另一个更新:

$(document).ready(function()
{
$("input[type='checkbox']").each(function() { 
 if($(this).attr("checked",true))
 {
        var id = mainid; 
        var value = this.value;
        var idn = this.id;
        if(id == "4") {
            $("input[type='checkbox']").each(function() {
                $(this).attr("disabled",true);
            });
        } else {
            $("input[type='checkbox']").each(function() {
                if(this.value == value) {
                    $(this).attr("disabled",true);
                    idn = ++id;
                }
            });
        }
}
});
});
4

1 回答 1

1

请使用以下代码供您使用,演示如下:

假设您从数据库中选择以下值

<?php
    $arrayIds = array("5"=>"1");
    //where 5 is id of the check box and 1 is value
?>
<script>
 $(document).ready(function(){
     <?php foreach($arrayIds as $key => $val ) { ?>
        var id = <?php echo $key; ?>;
        var value = <?php echo $val; ?>;
        var idn = id;
        if(id == "4") {
            $("input[type='checkbox']").each(function() {
                $(this).attr("disabled",true);
            });
        } else {
            $("input[type='checkbox']").each(function() {
                //alert(this.id+"--"+this.value+"--"+id+"--"+value+"--"+idn);
                //if(this.id == idn && this.value == value) {
                if(this.value == value) {
                    $(this).attr("disabled",true);
                    idn = ++id;
                }
            });
        }
     <?php } ?>
 });
</script>

上面的脚本将禁用所有值为 1 的复选框,即上述情况下的第一行。

如果我们得到关注

<?php
    $arrayIds = array("4"=>"1");
    //where 5 is id of the check box and 1 is value
?>
Above script will disable all the checkboxes found on the page.

hope this will help you complete your work

更新

试试这个

$(document).ready(function(){
    $("input[type='checkbox']").each(function() { 
        if($(this).attr("checked",true))  {
            var id = mainid; 
            var value = this.value;
            var idn = this.id;
            if(id == "4") {
                $("input[type='checkbox']").each(function() {
                    $(this).attr("disabled",true);
                });
            } else {
                $("input[type='checkbox']").each(function() {
                    if(this.value == value) {
                        $(this).attr("disabled",true);
                        idn = ++id;
                    }
                });
            }
        }
    });
});

你需要定义mainid虽然..

于 2013-10-03T08:01:07.070 回答