使用PHP在其他功能之前加载页面时更改复选框状态 [英] Change check box state on loading a page using PHP ahead of other functionality

查看:103
本文介绍了使用PHP在其他功能之前加载页面时更改复选框状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Ok so what I'm doing has several parts associated with it.

以下是涉及的步骤:


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

  1. I load a table of checkboxes using PHP (there's a table with 9 rows (identified by id from 4 to 12) and 3 columns (identified with value 1 to 3))

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

Once I load the page, the state of the checkboxes needs to change. If certain checkboxes are checked then certain others need to be disabled.

我使用jquery更改函数来查看已选中或取消选中的复选框。在这里,我根据用户选择在步骤2中使用相同的逻辑禁用或启用复选框。

I use a jquery change function to see which checkboxes have been checked or unchecked. Here I disable or enable the checkboxes using the same logic in step 2 depending on the user selection.

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

Each time a check box is checked or unchecked once the page has loaded I generate a JSON, push it into an array and post it to another page by clicking a submit button.

所以我很擅长步骤1,3和4.我需要帮助改变状态页面加载后复选框。

So I'm good with the steps 1,3 and 4. I need help with changing the state of the checkbox once the page has loaded.

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

How do I get the same logic I'm using to disable checkboxes, once there is a change in the checkbox state to work once the page has loaded?

这是我的JavaScript:

Here's my 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不等。

The id values vary from 4 to 12.

条件如下:

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

If any check box in row 5 to 12 are selected then all other check boxes in the same row should get disabled.

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

If any check box in the first row is selected then all the other check boxes in the table should get disabled.

另一个更新

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

So the check boxes will have the following values: [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]

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

There are varied possibilities so you may assume any check box is selected. But all the conditions should hold.

再次更新:

$(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;
                }
            });
        }
}
});
});


推荐答案

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

Please use following code for your use, demonstration as follows :

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

Suppose you get following values to be selected from database

<?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的复选框,即上述情况中的第一行。

Above script will disable all the checkboxes with value 1 i.e first row in above case.

如果我们得到以下

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



更新



试试这个

Update

Try this

$(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 ..

you need to define mainid though..

这篇关于使用PHP在其他功能之前加载页面时更改复选框状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆