简体   繁体   English

如何使用 codeigniter 将选中和未选中复选框的值更新为表

[英]How to update the value of checked and unchecked checkbox to table using codeigniter

var add_dynastfileds = function()
{
    CheckedFileds = [];
    UncheckedFileds = [];

    $(".DynFileds").each(function() {
        //console.log($(this).val());
        if ($(this).is(":checked")) {
            if(CheckedFileds.indexOf($(this).val())==-1){
                    CheckedFileds.push($(this).val());                  
                }
        } else {
                if(UncheckedFileds.indexOf($(this).val())==-1){
                    UncheckedFileds.push($(this).val());                  
                }
        }
    });
    $.post("<?=base_url()?>asset/addDynFields/",{CheckedFileds:CheckedFileds,UncheckedFileds:UncheckedFileds},function(result)
    {
        //console.log(result);
        $.gritter.add({
                        title: "Fields updated successfully ",
                        text: "",
                        time: 2000
        });
    });

}
<div class="panel-group">
    <div class="panel panel-default">
        <div class="row">
<?
    foreach($dynast_fields as $dynfileds){
        if(($dynfileds['display_name'] == 'Title') && 
            ($dynfileds['add_to_listing'] =='1'))
        {
            $styleClass = 'disabled="disabled"';
            $checked = "checked";
        }else{
            $styleClass = '';
            $checked = "";
        }
?>
                    <div class="col-md-1"></div>
                    <input type="checkbox" <?=$styleClass?> <?=$styleClass?> class="DynFileds" name="dynfileds<?=$dynfileds['rec_no']?>" id="dynfileds<?=$dynfileds['rec_no']?>" value="<?=$dynfileds['rec_no']?>" <? if($dynfileds['add_to_listing'] == '1') { echo "checked"; } ?>/>
                    <span style="font-size:12px;font-family: Open Sans,Helvetica Neue,Helvetica,Arial,sans-serif;"><?=$dynfileds['display_name']?></span>
                    <br/>
<?php 
    }
?>
        </div>
    </div>
    <span id='dup_checkdiv' style="display:none;" class="text-danger"></span>
    <br/>
    <div class="popover-footer" style="align:center;">
        <div class="row">
            <div class="col-md-3"></div>
            <div class="col-md-3">
                <a href="javascript:;" style="font-family: Open Sans,Helvetica Neue,Helvetica,Arial,sans-serif;" class="btn btn-info btn-sm" onclick="add_dynastfileds()">Save</a>
            </div>
        </div>
    </div>
</div>

public function addDynFields()
{
    $checkedfileds = $_POST['CheckedFileds'];
    $uncheckedFileds = $_POST['UncheckedFileds'];

    $this->db->update('pm1asset_dynamic_fields',array('add_to_listing' =>0));

    if(!empty($checkedfileds))
    {
        for($i = 0; $i < count($checkedfileds); $i++)
        {
            $data = array(
            'add_to_listing' =>1
            );
            $this->db->set($data);

            $this->db->where('rec_no',$checkedfileds[$i]);
            $this->db->update('pm1asset_dynamic_fields');
        }
    } 
}

Here I have written some code to update the value of checked and unchecked checkbox, for the first time it was working fine by uploading the value '0' to unchecked and the value '1' to checked checkbox, but after the page is reloaded, if i am going to update the value '0' by unchecked the previous checked checkbox, it's not going to update because after page is refreshed the checkbox value remains checked.在这里,我编写了一些代码来更新选中和未选中复选框的值,这是第一次通过将值“0”上传到未选中并将值“1”上传到选中复选框来正常工作,但是在页面重新加载后,如果我要通过取消选中先前选中的复选框来更新值“0”,则它不会更新,因为刷新页面后,复选框值仍处于选中状态。 This problem occurs due to i have give the condition in the checkbox of div tag, but i need this condition also.出现这个问题是因为我在 div 标签的复选框中给出了条件,但我也需要这个条件。 Is there any way to write it differently.有没有什么不同的写法。 how to resolve this problem can any one please tell me.如何解决这个问题任何人都可以告诉我。

Try something like this尝试这样的事情

$('.DynFileds:checked').each(function() {
   CheckedFileds.push(this.value);
});
$('.DynFileds:not(:checked)').each(function() {
   UncheckedFileds.push(this.value);
});

More更多的

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

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