简体   繁体   English

根据json数组值选择html复选框

[英]selecting html checkbox based on json array values

I have form where i am sending multiple checkbox items as array to controller,我有表单,我将多个复选框项目作为数组发送到控制器,

<?php
    foreach($groupsArray as $group)
        {
?>
<label>
<input type="checkbox" class="icheck" name="groups[]" id="groups" value="<?php echo $group["id"];?>"> <?php echo $group['name']?> </label>

<?php
}
?>

Everything works fine for checkbox values update in database,对于数据库中的复选框值更新,一切正常,

Now, what i am doing is, getting values from database and i need to check values which are stored in database,现在,我正在做的是,从数据库中获取值,我需要检查存储在数据库中的值,

I am getting below json response from PHP<我收到来自 PHP 的 json 响应<

groups: [{user_id: "2", group_id: "4", id: "4", name: "system Creators",…}]

Below i used as AJAX下面我用作 AJAX

if(objData.groups[0].group_id == $("#groups").val())
    {
        $("#groups").iCheck('check');
    }

With this $("#groups").val(), it always takes values of first checkbox, so there is problem, How can i compare values for all checkboxes with Json?有了这个 $("#groups").val(),它总是取第一个复选框的值,所以有问题,我如何用 Json 比较所有复选框的值? Also if groups array will have multiple values, means multidimensional array, more groups then?此外,如果组数组将具有多个值,则意味着多维数组,那么更多组呢?

Thanks in advance!提前致谢!

You need to be able to know which checkbox is related to which database value .您需要能够知道哪个checkbox与哪个database value

That is what the ID is used for on the checkbox .这就是IDcheckbox上的用途。 You have them all the same being "groups" - which is bad practice.你把它们都当作"groups" ——这是不好的做法。

Use: (note the dynamic id attribute)使用:(注意动态id属性)

<?php
    foreach($groupsArray as $group)
        {
?>
<label>
<input type="checkbox" class="icheck" name="groups[]" id="chk<?php echo $group["id"];?>" value="<?php echo $group["id"];?>"> <?php echo $group['name']?> </label>

<?php
}
?>

Then loop around your group object from the database:然后从数据库中循环您的组对象:

for(var i = 0; i < groups.length; i++) {
   var chk = document.getElementById("chk" + groups[i].id);
   chk.checked = true;
}

Try this:尝试这个:

PHP : PHP :

<?php
$count=0;
foreach($groupsArray as $group)
{
?>
    <label>
    <input type="checkbox" class="icheck" name="groups[]" id="groups<?php echo $count;?>" value="<?php echo $group["id"];?>"> <?php echo $group['name']; ?> </label>
<?php 
    $count++;
}
?>

Jquery :查询

for(var i=0;i<objData.gourps.length;i++){
    if(objData.groups[i].group_id == $("#groups"+i).val())
    {
        $("#groups"+i).iCheck('check');
    }
}

First, change the groups checkbox's id to it's class, so use class="groups" instead of id="groups" , like this:首先,将groups复选框的 id 更改为它的类,因此使用class="groups"而不是id="groups" ,如下所示:

<label><input type="checkbox" class="icheck" name="groups[]" class="groups" value="<?php echo $group["id"];?>"> <?php echo $group['name']?> </label>

Next, use the jQuery .each method to check all the checkboxes that is found in the JSON object like this:接下来,使用 jQuery .each方法检查在 JSON 对象中找到的所有复选框,如下所示:

$.each(objData, function(n, i){
    if(i.group_id == $(".groups[name="+n+"]").val()){
        $(".groups[name="+n+"]").iCheck('check');
    }
});

Finally, this worked!最后,这奏效了! Find only those checkboxes which are coming from DB and check those.只找到那些来自 DB 的复选框并选中它们。

for(var i = 0; i < objData.groups.length; i++) 
{
    $("#groups" + objData.groups[i].group_id).iCheck('check');
}

Thanks all for support!感谢大家的支持!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM